2010-08-11 66 views
1

我想通过表单上传文件,然后将其保存在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中没有任何内容。

任何形式的帮助将受到欢迎!

谢谢。

回答

5

这可以帮助:http://byatool.com/mvc/asp-net-mvc-upload-image-to-database-and-show-image-dynamically-using-a-view/

既然你在你的控制器方法有HttpPostedFileBase,所有你需要做的是:

int length = uploadFile.ContentLength; 
byte[] tempImage = new byte[length]; 
myDBObject.ContentType = uploadFile.ContentType; 

uploadFile.InputStream.Read(tempImage, 0, length); 
myDBObject.ActualImage = tempImage ; 

HttpPostedFileBase拥有的InputStream属性

希望这有助于。

+0

感谢这个答案,我会试一试,让你知道。谢谢! – 2010-08-11 19:17:42

+0

那么它的工作,它实际上很容易非常感谢你。 – 2010-08-13 13:59:59

2

好吧谢谢kheit,我最终得到它的工作。这是最终的解决方案,它可能会帮助那里的人。

这个脚本方法需要从目录中的所有文件,并将其上传到数据库中:

 //upload all file from a directory to the database as blob 
     public void UploadFilesToDB(long UniqueId) 
     { 
      //directory path 
      string fileUnformatedPath = "../Uploads/" + UniqueId; //setting final path with unique id 

      //getting all files in directory (if any) 
      string[] FileList = System.IO.Directory.GetFiles(HttpContext.Server.MapPath(fileUnformatedPath)); 

      //for each file in direcotry 
      foreach (var file in FileList) 
      { 
       //extracting file from directory 
       System.IO.FileStream CurFile = System.IO.File.Open(file, System.IO.FileMode.Open); 
       long fileLenght = CurFile.Length; 

       //converting file to a byte array (byte[]) 
       byte[] tempFile = new byte[fileLenght]; 
       CurFile.Read(tempFile, 0, Convert.ToInt32(fileLenght)); 

       //creating new attachment 
       IW_Attachment CurAttachment = new IW_Attachment(); 
       CurAttachment.attachment_blob = tempFile; //setting actual file 

       string[] filedirlist = CurFile.Name.Split('\\');//setting file name 
       CurAttachment.attachment_name = filedirlist.ElementAt(filedirlist.Count() - 1);//setting file name 

       //uploadind attachment to database 
       SubmissionRepository.CreateAttachment(CurAttachment); 

       //deleting current file fromd directory 
       CurFile.Flush(); 
       System.IO.File.Delete(file); 
       CurFile.Close(); 
      } 

      //deleting directory , it should be empty by now 
      System.IO.Directory.Delete(HttpContext.Server.MapPath(fileUnformatedPath)); 
     } 

(顺便说IW_Attachment是我的数据库表的名字之一)