C# 上传的 HttpPostedFile 为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/544182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 07:34:49  来源:igfitidea点击:

Uploaded HttpPostedFile is null

c#asp.netasp.net-mvc

提问by

On the View:

在视图上:

<% =Html.BeginForm("About", "Home", FormMethod.Post, new {enctype="multipart/form-data "})%>
  <input type="file" name="postedFile" />
  <input type="submit" name="upload" value="Upload" />
<% Html.EndForm(); %>

In the Controller, there's something like this:

在控制器中,有这样的东西:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult About(HttpPostedFile postedFile)
{
    //but postedFile is null 
    View();
}

postedFileis null in About(). How do I upload the file?

postedFile在 About() 中为 null。如何上传文件?

回答by Ben Robbins

This doesn't answer why your parameter is null, but you can dig into the request directly. This might not be the most "MVC" way of doing this though. try this in your method body:

这并没有回答为什么您的参数为空,但您可以直接深入研究请求。不过,这可能不是最“MVC”的方式。在你的方法体中试试这个:

var upload = Request.Files["postedFile"]
if (upload.ContentLength > 0)
{
  // Do whatever
}

To be more "MVC, "You could pull that code out of your controller into an IModelBinder implementation and using a custom object as a parameter to your method. This Scott Hanselman blog postshows the steps to implement a custom ModelBinder.

为了更“MVC”,您可以将该代码从控制器中提取到 IModelBinder 实现中,并使用自定义对象作为方法的参数。这篇Scott Hanselman 博客文章展示了实现自定义 ModelBinder 的步骤。

回答by Pita.O

I also get some quirks with <%= Html.BeginForm ...%>. So, I use the using. Again, on the Controller side, I just grab my uploaded files form the request object.

我也有一些怪癖<%= Html.BeginForm ...%>。所以,我使用 using。同样,在控制器端,我只是从请求对象中获取我上传的文件。

Try this. It works:

尝试这个。有用:

<% using (Html.BeginForm("Post", "Home", FormMethod.Post, new {enctype = "multipart/form-data"}))
   {%>

    <input type="file" id="postedFile" name="PostedFile" />
    <input type="submit" />

<%
}

%>

%>

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Post(FormCollection form)
    {
      HttpPostedFileBase postedFile =   Request.Files["PostedFile"];
        return RedirectToAction("Index");
    }

回答by Min

Which version of MVC are you using? Right now with the RC candidate I tried using HttpPostedFile and I got an "does not have a blank constructor error." I had to use HttpPostedFileBase.

您使用的是哪个版本的 MVC?现在与 RC 候选人一起我尝试使用 HttpPostedFile 并且我得到一个“没有空白的构造函数错误”。我不得不使用 HttpPostedFileBase。

More importantly though, is the version of MVC you're running on, depending on the version, how your retrieve a posted file will be different.

更重要的是,您正在运行的 MVC 版本取决于版本,您检索已发布文件的方式将有所不同。

回答by Carl H?rberg

Use HttpPostedFileBase (not HttpPostedFile) and also name the parameter exactly as in the form. eg. if you have

使用 HttpPostedFileBase(而不是 HttpPostedFile)并完全按照表单命名参数。例如。如果你有

<input type="file" id="file1" name="file1" />

you have to have the method head:

你必须有方法头:

public ActionResult About(HttpPostedFileBase file1)

回答by Stefan Walther

I had the same problem:

我有同样的问题:

You have to define a name AND id for the input element:

您必须为输入元素定义名称和 ID:

<input type="file" name="postedFile" id="postedFileId"  /> 

Best regards

此致

Stefan

斯特凡

回答by Mihai Marandici

You should remove the space from the end of enctype attribute:

您应该从 enctype 属性的末尾删除空格:

new {enctype="multipart/form-data "} => new {enctype="multipart/form-data"}

回答by Prakash

An empty space after "multipart/form-data " is the real issue...

“multipart/form-data”之后的空白是真正的问题......

回答by Rafael Marques

are you using jquery mobile? if yes, you'll need to set @data_ajax to false

你在使用 jquery mobile 吗?如果是,您需要将@data_ajax 设置为 false

new { enctype = "multipart/form-data", @data_ajax = "false" }

回答by MikeUpjohn

Additionally, I have found that the form declaration in which the file upload lies must always have

此外,我发现文件上传所在的表单声明必须始终具有

new {enctype="multipart/form-data"}

回答by ernesto petit

use this

用这个

public ActionResult Upload(HttpPostedFileBase excelfile)

change HttpPostedFilefor HttpPostedFileBase

改变HttpPostedFileHttpPostedFileBase