在 ASP.NET MVC 中上传文件(再次!)
我在 asp.net mvc 2 中上传文件时遇到问题。 我的控制器函数的参数是 FormCollection
类型。由于字段太多,我无法将每个字段分开作为参数。我的表单中有 2 个上传文件字段。如何在我的控制器中获取上传的文件?
我尝试了这种方式:
public ActionResult CreateAgent(FormCollection collection, HttpPostedFileBase personImage)
{
...
}
但是 personImage
是 null
。 :(
或者这样:
HttpPostedFileBase img = this.HttpContext.Request.Files[collection["personImage"]];
但 img
是 null
另外,collection["personImage"]
是所选文件的名称(没有路径),我无法将其转换为 HttpPostedFileBase
请注意,必须填写所有字段 。在一页上。我不能让客户在单独的页面上传图片!
I have a problem with uploading file in asp.net mvc 2.
My controller function's parameter is a FormCollection
type. Because the fields are too numerous, I can't separate each field as a parameter. I have 2 upload file fields in my form. How can I get uploaded files in my controller?
I tried this way:
public ActionResult CreateAgent(FormCollection collection, HttpPostedFileBase personImage)
{
...
}
but personImage
was null
. :(
or this way:
HttpPostedFileBase img = this.HttpContext.Request.Files[collection["personImage"]];
but img
was null
to. Also collection["personImage"]
was the name of selected file (without path) and I can't cast it to HttpPostedFileBase
.
Note that all fields must be filled in on one page. I can't let the customer upload images in a separate page!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先阅读这篇博文。然后将其应用到您的场景:
用 WebForms 语言翻译后给出:
然后:
如果您有很多文件,则使用
IEnumerable
,如 Haacked 所示。备注:
this.HttpContext.Request.Files
this.HttpContext.Request.Files[collection["personImage"]]< /code> 在 ASP.NET MVC 应用程序中。
Start by reading this blog post. Then apply it to your scenario:
which translated in WebForms language gives:
and then:
If you have many files then use
IEnumerable<HttpPostedFileBase>
as illustrated by Haacked.Remarks:
this.HttpContext.Request.Files
in an ASP.NET MVC applicationthis.HttpContext.Request.Files[collection["personImage"]]
in an ASP.NET MVC application.在您看来,您的表单的 using 语句是什么样的?它应该看起来像这样:
What does your using statement look like in your view for the form? It should look something like this: