2010-08-11 63 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是我的數據庫表的名字之一)