2009-04-19 159 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....; 
     } 

    }