在 ASP.NET MVC 中上传文件(再次!)

发布于 2024-10-31 19:17:46 字数 651 浏览 1 评论 0原文

我在 asp.net mvc 2 中上传文件时遇到问题。 我的控制器函数的参数是 FormCollection 类型。由于字段太多,我无法将每个字段分开作为参数。我的表单中有 2 个上传文件字段。如何在我的控制器中获取上传的文件?

我尝试了这种方式:

public ActionResult CreateAgent(FormCollection collection, HttpPostedFileBase personImage)
{
    ...
}

但是 personImagenull。 :(

或者这样:

HttpPostedFileBase img = this.HttpContext.Request.Files[collection["personImage"]];

imgnull另外,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人 2024-11-07 19:17:46

首先阅读这篇博文。然后将其应用到您的场景:

<form action="/Home/CreateAgent" method="post" enctype="multipart/form-data">

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

    ... Some other input fields for which we don't care at the moment
        and for which you definetely should create a view model
        instead of using FormCollection in your controller action

    <input type="submit" />
</form>

用 WebForms 语言翻译后给出:

<% using (Html.BeginForm("CreateAgent", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) { %>
    <input type="file" name="file1" id="file" />
    <input type="file" name="file2" id="file" />

    ... Some other input fields for which we don't care at the moment
        and for which you definetely should create a view model
        instead of using FormCollection in your controller action

    <input type="submit" />
<% } %>

然后:

public ActionResult CreateAgent(
    // TODO: To be replaced by a strongly typed view model as the 
    // ugliness of FormCollection is indescribable
    FormCollection collection, 
    HttpPostedFileBase file1, 
    HttpPostedFileBase file2
)
{
    // use file1 and file2 here which are the names of the corresponding
    // form input fields
}

如果您有很多文件,则使用 IEnumerable,如 Haacked 所示。

备注:

  • 绝对永远不要在 ASP.NET MVC 应用程序中使用 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:

<form action="/Home/CreateAgent" method="post" enctype="multipart/form-data">

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

    ... Some other input fields for which we don't care at the moment
        and for which you definetely should create a view model
        instead of using FormCollection in your controller action

    <input type="submit" />
</form>

which translated in WebForms language gives:

<% using (Html.BeginForm("CreateAgent", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) { %>
    <input type="file" name="file1" id="file" />
    <input type="file" name="file2" id="file" />

    ... Some other input fields for which we don't care at the moment
        and for which you definetely should create a view model
        instead of using FormCollection in your controller action

    <input type="submit" />
<% } %>

and then:

public ActionResult CreateAgent(
    // TODO: To be replaced by a strongly typed view model as the 
    // ugliness of FormCollection is indescribable
    FormCollection collection, 
    HttpPostedFileBase file1, 
    HttpPostedFileBase file2
)
{
    // use file1 and file2 here which are the names of the corresponding
    // form input fields
}

If you have many files then use IEnumerable<HttpPostedFileBase> as illustrated by Haacked.

Remarks:

  • Absolutely never use this.HttpContext.Request.Files in an ASP.NET MVC application
  • Absolutely never never never use this.HttpContext.Request.Files[collection["personImage"]] in an ASP.NET MVC application.
笑叹一世浮沉 2024-11-07 19:17:46

在您看来,您的表单的 using 语句是什么样的?它应该看起来像这样:

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

What does your using statement look like in your view for the form? It should look something like this:

using (Html.BeginForm("CreateAgent", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文