2013-04-11 29 views
0

以下是我的形式上傳圖像到服務器阿賈克斯

<form id="postProblemForm" action="/Problems/Post" method="post"> 
<input type="text" id="problemSubject" name="problemSubject" class="inp-form"/> 
<input type="file" id="uploadFile" name="uploadFile"/> 
<textarea rows="" cols="" class="form-textarea" id="problemDescription" name="problemDescription"></textarea> 
</form> 

我必須填寫此表並控制器。控制器代碼是

[HttpPost] 

     public void Post(FormCollection form) 
     { 
      string subject = form["problemSubject"]; 
      string description = form["problemDescription"]; 
      HttpPostedFileBase photo = Request.Files["uploadFile"]; 

     } 

我可以很容易地得到「problemSubject」和「problemDescription」值。但不知道如何獲取上傳的圖像並將其保存在服務器端。我想保存在「〜/ ProblemImages」。請幫助。

+1

您是否諮詢過Google或MDN? – gdoron 2013-04-11 05:19:53

+0

是的。但不滿意。 – 2013-04-11 05:21:44

+0

你的搜索關鍵字是什麼?你有沒有嘗試類似於:'在谷歌中用jQuery上傳文件?我相信你現在已經得到了答案。 – gdoron 2013-04-11 05:23:36

回答

0

您無法通過的FormCollection文件傳輸到控制器,你需要使用這樣的事情:

 [HttpPost] 
     public ActionResult Save(IEnumerable<HttpPostedFileBase> attachments) 
     { 
      // The Name of the Upload component is "attachments" 
      foreach (var file in attachments) 
      { 
       // Some browsers send file names with full path. This needs to be stripped. 
       var fileName = Path.GetFileName(file.FileName); 
       var physicalPath = Path.Combine(Server.MapPath("~/App_Data"), fileName); 

       file.SaveAs(physicalPath); 
      } 
      // Return an empty string to signify success 
      return Content(""); 
     } 

,你也沒有任何插件獲取幫助做這件事是不是一個好主意,我用了幾插件和我發現劍道UI上傳插件不錯,這裏是一個鏈接它是如何工作的:

http://demos.kendoui.com/web/upload/async.html

,你可以在這裏找到示例項目: http://www.kendoui.com/forums/ui/upload/upoad-with-mvc.aspx