#眉標=ASP.NET MVC、MVC Futures、LINQ #副標=ASP.NET網站開發與應用(8) #大標=非同步處理使用者上傳的檔案 #作者=文/圖 王寧疆 ========<反灰>=============== using Microsoft.Web.Mvc; protected void Application_Start() { RegisterRoutes(RouteTable.Routes); FileCollectionModelBinder.RegisterBinder( ModelBinders.Binders); //註冊FileCollectionModelBinder } ======================= ========<反灰>=============== public ActionResult UploadFile() { return View(); //顯示使用者選擇欲上傳的檔案內容 } [AcceptVerbs(HttpVerbs.Post)] public ActionResult UploadFile(HttpPostedFileBase[] files) { int Count = 0; //記錄上傳次數的變數 foreach (HttpPostedFileBase file in files) //取出每一個被上傳的檔案 { if (file != null) //若確實有選擇欲上傳的檔案 { string fileNameOnDisk = Path.Combine(Server.MapPath("~/Uploads"), Path.GetFileName(file.FileName));//準備存放被上傳的檔案的資料夾與檔案名稱 file.SaveAs(fileNameOnDisk); //將使用者上傳的檔案存放到指定的資料夾中 Count++; //遞增記錄上傳檔案數目的變數的內容值 } ViewData["status"] = string.Format("{0} 個檔案上傳成功!", Count); //準備檔案上傳的結果資訊供View顯示 } return View(); //顯示檔案上傳的結果畫面 } ======================= ========<反灰>===============

檔案上傳

<% using( var form = Html.BeginForm( "UploadFile", "Home", FormMethod.Post, new { enctype = "multipart/form-data"})) {%>




<% }%> <%= Html.Encode(ViewData["status"]) %> ======================= ========<反灰>=============== ======================= ========<反灰>=============== routes.MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "" } ); //修改如下 routes.MapAsyncRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "" } // Parameter defaults ); ======================= ========<反灰>=============== //修改如下 ======================= ========<反灰>=============== //修改如下 ======================= ========<反灰>=============== public class HomeController : AsyncController { //原有的程式碼 } ======================= ========<反灰>=============== public ActionResult ReadFile() //以同步的方式處理使用者對讀取檔案內容 { byte[] buf = new byte[1024]; //建立存放檔案內容的陣列 FileStream fs = new FileStream(Server.MapPath("~/DtcInstall.log"), FileMode.Open, FileAccess.Read); //建立物件並開啟DtcInstall.log fs.Read(buf, 0, buf.Length); //檔案內容讀入到buf ViewData["FileContent"] = Encoding.Default.GetString(buf); //陣列內容轉成字串後存到ViewData集合 return View("ReadFile"); //命令ReadFile顯示執行結果 } public void ReadFileAsync() //以非同步的方式處理使用者對讀取檔案內容的需求 { AsyncManager.OutstandingOperations.Increment(); //遞增未完成的非同步工作數量,從執行緒集區取出執行緒執行讀取檔案內容的耗時工作  ThreadPool.QueueUserWorkItem(callback => { byte[] buf = new byte[1024]; //建立存放讀取到的檔案內容的byte陣列 FileStream fs = new FileStream(Server.MapPath("~/DtcInstall.log"), FileMode.Open, FileAccess.Read); //建立FileStream類別的物件並開啟名稱為DtcInstall.log的檔案   fs.Read(buf, 0, buf.Length); //將檔案內容讀入到名稱為buf的byte陣列 AsyncManager.Parameters["buf"] = buf; //將byte陣列的內容放入名稱為buf的參數 AsyncManager.OutstandingOperations.Decrement(); //遞增未完成的非同步工作數量 }, null); } public ActionResult ReadFileAsyncCompleted(byte[] buf) //處理非同步工作執行結果的方法 {  ViewData["FileContent"] =  Encoding.Default.GetString(buf); //將byte陣列的內容轉成字串後存放到ViewData集合 return View("ReadFile"); //命令名稱為ReadFile的View顯示執行結果 } ======================= ========<反灰>===============

ReadFile

<%= Html.Encode(ViewData["FileContent"]) %> ======================= ========<反灰>=============== ======================= ========<反灰>=============== ======================= ========<反灰>=============== public ActionResult GetShippers() { NorthwindDataContext dc = new NorthwindDataContext(); //建立DataContext物件 var query = from s in dc.Shippers select s; //查詢Shippers資料表的記錄 ViewData["ShipperItems"] = query.ToList(); //將查詢得到的結果放入ViewData集合 return View(); } ======================= ========<反灰>===============

貨運資訊

ShipperID:
CompanyName:
Phone:

=======================