【问题标题】:Upload file in ASP.NET MVC (Again!)在 ASP.NET MVC 中上传文件(再次!)
【发布时间】:2011-08-03 09:34:06
【问题描述】:

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

我试过这样:

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

personImagenull。 :(

或者这样:

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

imgnullcollection["personImage"] 也是所选文件的名称(没有路径),我无法将其转换为 HttpPostedFileBase

请注意,所有字段都必须在一页上填写。我不能让客户在单独的页面中上传图片!

【问题讨论】:

    标签: .net asp.net-mvc-2 file-upload upload httppostedfile


    【解决方案1】:

    首先阅读this blog post。然后将其应用到您的场景中:

    <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&lt;HttpPostedFileBase&gt;,如 Haacked 所示。

    备注:

    • 绝对不要在 ASP.NET MVC 应用程序中使用 this.HttpContext.Request.Files
    • 绝对永远永远永远不要在 ASP.NET MVC 应用程序中使用 this.HttpContext.Request.Files[collection["personImage"]]

    【讨论】:

    • 你的意思是我们永远不应该使用this.HttpContext.Request
    • @Jalal Amini,你不应该使用this.HttpContext.Request.Files。顺便说一句,由于默认的模型绑定器,您甚至可以避免使用 this.HttpContext.Request
    【解决方案2】:

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

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

    【讨论】:

    • 是的:&lt;% using (Html.BeginForm("CreateAgent", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))%&gt;
    • @Jalai Amini 您的文件输入名称是否与 CreateAgent 签名上的参数匹配?
    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 2019-01-29
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多