2014-06-15 20 views
1

我一拖&下拉功能在我的應用圖片:
好像文件作爲HttpPostedFileBase保存與此代碼:保存HttpPostedFileBase在文件夾中的圖像

public ActionResult SaveUploadedFile() 
{ 
    bool isSavedSuccessfully = true; 

    foreach (string fileName in Request.Files) 
    { 
     HttpPostedFileBase file = Request.Files[fileName]; 
    } 
}  

我的最終目標是將圖像路徑存儲在我的數據庫中,但現在我會很高興,如果我可以 將此HttpPostedFileBase文件轉換爲JPG並將其存儲在我的項目中的圖像文件夾中。有關如何解決這個問題的任何提示?

編輯: 此代碼似乎到了HttpPostedFileBase轉換爲圖像:

public ActionResult SaveUploadedFile() 
{ 
    bool isSavedSuccessfully = true; 

    foreach (string fileName in Request.Files) 
    { 
     HttpPostedFileBase file = Request.Files[fileName]; 
     var filename = Path.GetFileName(file.FileName); 

      var sourceimage = System.Drawing.Image.FromStream(file.InputStream); 
      img = sourceimage; 
    } 
} 

我還添加了道具,將其存儲在:

public Image img { get; set; } 

編輯:

public HttpPostedFileBase DropImg { get; set; } 
    public Image img { get; set; } 

public ActionResult SaveUploadedFile(string test) 
     { 
      bool isSavedSuccessfully = true; 

      foreach (string fileName in Request.Files) 
      { 
       HttpPostedFileBase file = Request.Files[fileName]; 
       DropImg = file; 
       var filename = Path.GetFileName(file.FileName); 

       var sourceimage = System.Drawing.Image.FromStream(file.InputStream); 
       img = sourceimage; 

       var fullPath = "~/Content/images/drops/" + file.FileName; 
       file.SaveAs(Server.MapPath(fullPath)); 
      } 




      if (isSavedSuccessfully) 
      { 
       return Json(new { Message = "File saved" }); 

      } 
      else 
      { 
       return Json(new { Message = "Error in saving file" }); 
      } 


     } 

我hping,這將保存我的圖像到文件夾滴...我錯過了什麼?

回答

1
var fullPath = "~/UploadedImages/" + file.FileName; 
file.SaveAs(Server.MapPath(fullPath)); 
+0

謝謝你試圖幫助我,但我無法得到它的工作,看到我的代碼更新與你添加了一分鐘。 – user2915962

相關問題