【问题标题】:File upload MVC文件上传MVC
【发布时间】:2009-04-19 10:43:57
【问题描述】:

在我看来有以下标记:

<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 上安装了 VS2008,MVC 1.0。

为什么?

马尔科姆

【问题讨论】:

  • "没有人会得到答案" - ???
  • 嗯,给出的 2 个答案没有,我把钱放在没有人解决它的问题上。
  • 答案是正确解决问题的方法
  • 请不要以为社区会让你失望。

标签: asp.net-mvc


【解决方案1】:

使用HttpPostedFileBase 作为您操作的参数。另外,添加AcceptVerb属性设置为POST

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

这段代码非常符合 ASP.NET MVC 的精神/设计。

【讨论】:

  • 我只花了几个小时转圈,因为我的文件输入标签有一个“ID=”属性,但没有“NAME=”-确保包含“name=...”或该文件将发布到 actionresult,但将为空。希望这对某人有所帮助。
【解决方案2】:

在这里不要挑剔或任何东西,但这是代码应该看起来的样子,因为 Daniel 在他提供的代码中缺少一些小细节......

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

    return View();
}

【讨论】:

  • 我猜丹尼尔错过了一些东西,但看到了布雷特的帖子并修改了他的答案。
  • 语法警察! "UploadPlotImadge" >>> "UploadPlotImage" 哈哈 =)
【解决方案3】:

试试这个代码:

    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);
        }

    ...
    }

【讨论】:

  • 您不需要 Request.Files。看到这个答案:stackoverflow.com/questions/765211/file-upload-mvc/…
  • 如果您打算处理多个文件上传,则需要 Request.Files。
  • 如果您发布其他表单字段,您还需要 Request.Files。很好的解决方案佩德罗。
【解决方案4】:

即使我遇到了问题,图像中的值为 null

public ActionResult UploadPlotImadge(HttpPostedFileBase image) 

之前我没有添加我添加的[AcceptVerbs(HttpVerbs.Post)]。即使添加了它,它也不起作用,因为我缺少的第二部分,enctype="multipart/form-data",需要在表单标签中..

现在它对我有用....

【讨论】:

  • enctype="multipart/form-data" 对我来说也很重要,为什么不是每个人都需要这个?
【解决方案5】:

尝试此类及以下操作并修复 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;
    }
}

//发布动作

    [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....;
        }

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    • 2011-07-08
    • 2013-08-31
    • 2013-06-10
    • 1970-01-01
    相关资源
    最近更新 更多