我想通过表单上传文件,然后将其保存在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