2009-04-19 81 views
9

隨着在我看來,下面的標記:文件上傳MVC

<form action="Categories/Upload" enctype="multipart/form-data" method="post"> 
    <input type="file" name="Image"> 
    <input type="submit" value"Save"> 
</form> 

而且在我的控制器:

public ActionResult Upload(FormCollection form) 
{ 
    var file = form["Image"]; 
} 

文件的價值是null。 如果我使用不同的控制器控制器在不同的視圖中嘗試它,並且它使用相同的代碼。

我有Vista,MVC 1.0 VS2008。

爲什麼?

馬爾科姆

+7

「沒有人會有一個答案「 - ? – 2009-04-19 10:46:01

+0

那麼給出的2個答案不對,我把錢放在沒有人解決它。 – Malcolm 2009-04-19 11:50:01

+0

答案是解決問題的權利 – Malcolm 2009-04-19 11:50:35

回答

6

試試這個代碼:

public ActionResult Upload() 
    { 
     foreach (string file in Request.Files) 
     { 
      var hpf = this.Request.Files[file]; 
      if (hpf.ContentLength == 0) 
      { 
       continue; 
      } 

      string savedFileName = Path.Combine(
       AppDomain.CurrentDomain.BaseDirectory, "PutYourUploadDirectoryHere"); 
       savedFileName = Path.Combine(savedFileName, Path.GetFileName(hpf.FileName)); 

      hpf.SaveAs(savedFileName); 
     } 

    ... 
    } 
+0

否請求。文件是空的? – Malcolm 2009-04-19 11:48:57

34

使用HttpPostedFileBase作爲您的操作參數。另外,將AcceptVerb屬性設置爲POST

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Upload(HttpPostedFileBase image) 
{ 
    if (image != null) { 
     // do something 
    } 
    return View(); 
} 

這段代碼在ASP.NET MVC的精神/設計上很有意思。

7

這裏不作挑剔或任何東西,但這裏的代碼如何應該看看,因爲丹尼爾在他提供的代碼所缺少一些小的細節...

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult UploadPlotImage(HttpPostedFileBase image) 
{  
    if (image != null) 
    {   
     // do something  
    } 

    return View(); 
} 
2

即使我面臨的一個問題,該值在圖像空在

public ActionResult UploadPlotImadge(HttpPostedFileBase image) 

早些時候,我沒加[AcceptVerbs(HttpVerbs.Post)]我補充道。即使加入之後,也沒有工作,因爲我錯過了,enctype="multipart/form-data"第二部分,需要在窗體標籤..

現在它爲我....

0

嘗試這個類和下面的操作並修復AppSetting中的文件夾路徑。

配置:

<appSettings> 
      <add key="UploadFolerPath" value="..Your folder path" /> 
    </appSettings> 

觀點: -

<form action="Account/AddImage" id="form_AddImage" method="post" enctype="multipart/form-data"> 

      <input type="file" id="Img" name="Img" class="required" /> 

      <input type="submit" value="Upload" id="btnSubmit" /> 

</form> 

類: -

public class FileUpload 
{ 
    public string SaveFileName 
    { 
     get; 
     set; 
    } 


    public bool SaveFile(HttpPostedFileBase file, string FullPath) 
    { 
     string FileName = Guid.NewGuid().ToString(); 

     FileName = FileName + System.IO.Path.GetExtension(file.FileName); 

     SaveFileName = FileName; 

     file.SaveAs(FullPath + "/" + FileName); 
     return true; 
    } 
} 

// POST操作

[HttpPost] 
    public ActionResult AddImage(FormCollection Form) 
    { 

     FileUpload fileupload = new FileUpload(); 
     var image=""; 

     HttpPostedFileBase file = Request.Files["Img"]; 

     if (file.FileName != null && file.FileName != "") 
     { 

      if (upload.ContentLength > 0) 
      { 

        fileupload.SaveFile(Request.Files["Img"], Server.MapPath(AppSetting.ReadAppSetting("UploadFolerPath"))); 

       image = fileupload.SaveFileName; 

       // write here your Add/Save function 

       return Content(image); 


      } 
     } 
     else 
     { 
        //return....; 
     } 

    }