我想通過表單上傳文件,然後將其保存在SQL中作爲blob。ASP MVC 2將文件上傳到數據庫(blob)
我已經有我的形式工作的罰款,我的數據庫是完全可以採取的斑點,我有一個控制器,拿文件,在本地目錄中保存它:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult FileUpload(int id, HttpPostedFileBase uploadFile)
{
//allowed types
string typesNonFormatted = "text/plain,application/msword,application/pdf,image/jpeg,image/png,image/gif";
string[] types = typesNonFormatted.Split(',');
//
//Starting security check
//checking file size
if (uploadFile.ContentLength == 0 && uploadFile.ContentLength > 10000000)
ViewData["StatusMsg"] = "Could not upload: File too big (max size 10mb) or error while transfering the file.";
//checking file type
else if(types.Contains(uploadFile.ContentType) == false)
ViewData["StatusMsg"] = "Could not upload: Illigal file type!<br/> Allowed types: images, Ms Word documents, PDF, plain text files.";
//Passed all security checks
else
{
string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"),
Path.GetFileName(uploadFile.FileName)); //generating path
uploadFile.SaveAs(filePath); //saving file to final destination
ViewData["StatusMsg"] = "Uploaded: " + uploadFile.FileName + " (" + Convert.ToDecimal(uploadFile.ContentLength)/1000 + " kb)";
//saving file to database
//
//MISSING
}
return View("FileUpload", null);
}
現在我很想念將文件放入數據庫中。我找不到任何關於此主題的內容......我找到了一些在常規網站中執行此操作的方法,但MVC2中沒有任何內容。
任何形式的幫助將受到歡迎!
謝謝。
感謝這個答案,我會試一試,讓你知道。謝謝! – 2010-08-11 19:17:42
那麼它的工作,它實際上很容易非常感謝你。 – 2010-08-13 13:59:59