我剛開始學習ASP.NET MVC。我正在處理的網站要求用戶上傳通過NHibernate加載到SQL數據庫中的XML文件。這對於一個小XML文件現在可以正常工作,但是一旦我嘗試了一個更大的文件,我開始出現問題。如何設置ASP.NET MVC來處理獨立於請求的上傳數據?
當我上傳一個較大的文件時,需要一段時間才能將數據寫入數據庫,因此在用戶的末端它們會卡住「等待本地主機......」,直到數據庫寫入完成並且Upload()
動作方法返回一個視圖。
相反,我想設置一些類似大多數熱門電影上傳網站的內容,一旦用戶上傳文件,他們會立即發送到另一個頁面,並顯示類似「正在處理您的上傳內容」的消息晚一點回來」。
我已經做了多線程的一點點,但我很難感覺我知道我在做什麼。雖然,剛纔我似乎已經按照我想要的方式使用System.Threading.ThreadPool
來獲得我的網站。
像這樣使用線程池是解決這個問題的最好方法嗎?
這是我的Upload()操作方法。
public ActionResult Upload(int id)
{
//check the type of request
if (Request.RequestType == "GET")
{
//get GET show the upload form
return View();
}
else if (Request.RequestType == "POST")
{
//if POST process uploaded file data
if (Request.Files.Count == 1)
{
//copy the upload stream into a memory stream
System.IO.MemoryStream memStream = new System.IO.MemoryStream();
Request.Files[0].InputStream.CopyTo(memStream);
//send the the memorystream, id, and username to a new Thread pool work item
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(WriteUploadToDb), new WriteUploadToDbData(id, memStream,User.Identity.Name));
//send the user to a page
ViewBag.Message = "Your file is being processed. Please come back later...";
return RedirectToAction("Reports", new { ID = id });
}
else
{
throw new Exception("Multiple report files uploaded!");
}
}
}
這裏是我在ThreadPool上運行的WriteUploadToDb()方法。
private static void WriteUploadToDb(object obj)
{
WriteUploadToDbData updateData = (WriteUploadToDbData)obj;
//resets the stream position
updateData.Data.Position = 0;
//creates a new Nhibernate session
using (var session = MvcApplication.SessionFactory.OpenSession())
{
using (var trans = session.BeginTransaction())
{
var proj = session.Get<Project>(updateData.ProjectID);
//adds the xml data into the Project
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(updateData.Data);
proj.AddReport(xmlDoc, updateData.UserName);
//saves the project to the db
session.Update(proj);
trans.Commit();
}
}
//disposes the memory stream
updateData.Data.Dispose();
}
在可預見的將來,我不認爲這將成爲高容量的網站。大多數情況下,這僅僅是我學習ASP.NET MVC的一個項目。我猜想另一種選擇也可能是將用戶上傳的文件轉儲到另一個程序正在監控的文件夾中? – 2011-04-26 01:25:25
我同意MSMQ,也考慮Windows Azure隊列。 – BZink 2011-04-26 03:00:22
@Eric答:是的,您可以將每個文件單獨轉儲到一個文件夾中。另一個進程(例如Windows服務)可以提取這些文件。 Windows Service主要可以在另一臺服務器上存在,並通過網絡共享訪問該文件。如果有人在捲上執行該操作,則可能需要Web服務器中有2個NIC ......一個用於處理網絡流量,另一個用於文件複製/遠程文件訪問操作。 – 2011-04-26 03:51:53