上载多个文件-MVC ..是否有总文件大小限制? [英] Uploading Multiple Files - MVC..is there a total file size limit?

查看:105
本文介绍了上载多个文件-MVC ..是否有总文件大小限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,用户可以从中添加属性列表.除了用户必须输入的数据之外,用户还可以上传许多图像.我正在使用HTML5多重属性,允许用户一次上传多个文件.

I have a form from which users can add a property listing. Apart from the data that the user has to enter, the user can also upload a number of images. I'm using the HTML5 multiple attribute to allow users to upload more than one file at once.

由于某种原因,当我上传一张图片时,我总是会触发HttpPost Add方法.但是,当我尝试上传多张图片时,该方法不会触发.到目前为止,我还没有收到任何错误,所以我不确定我的实现有什么问题.

For some reason the HttpPost Add method I have always fires when I upload one image. However, when I try to upload multiple images the method does not fire. Up till now I'm not getting any errors so I'm not sure what is wrong with my implementation.

HTML

@using (Html.BeginForm("AddProperty", "User", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <fieldset>
        <legend>Property</legend>

        <div class="editor-label">
            Title
        </div>
        <div class="editor-field">
            <input type="text" name="title" />
        </div>

        <div class="editor-label">
            Description
        </div>
        <div class="editor-field">
            <input type="text" name="desc" />
        </div>

        @* ... more input fields... *@

        <div class="editor-label">
            Images
        </div>
        <div class="editor-field">
            <input type="file" multiple="multiple" name="file" />
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

控制器

[HttpPost]
public ActionResult AddProperty(FormCollection form, IEnumerable<HttpPostedFileBase> file)
{
    Property p = new Property();
    p.Title = form["title"];
    p.Description = form["desc"];

    // ... setting Property fields

    new PropertiesBL().AddProperty(p);

    for (int i = 0; i < file.Count(); i++)
    {
        // ... loop and save uploaded file(s)
    }
}

任何帮助将不胜感激.

编辑-进一步测试

为了测试文件上传,我创建了一个仅包含文件上传器的表单的新视图.我尝试上传不同的文件类型.我注意到的是,例如,在上传10个文档时,将触发Upload方法.但是,当尝试上传3张(大)图像时,它没有触发.我认为上传的总字节数存在某种限制...我不确定这是否有意义.

To test the file upload I've created a new view with just a form containing a file uploader. I've tried uploading different file types. What I've noted is that when uploading for example 10 documents the Upload method fired. But when trying to upload 3 (large) images it didn't fire. I'm thinking that there's some kind of limit with regards to the total number of bytes being uploaded...I'm not sure if this makes sense though.

简单测试

<form method="post" action="/Test/Upload" enctype="multipart/form-data">
    <input type="file" multiple="multiple" name="images" />
    <input type="submit" value="Upload" />
</form>

[HttpPost]
public ActionResult Upload(IEnumerable<HttpPostedFileBase> images)
{
    return RedirectToAction("Index");
}

我也尝试过分别上传多个文件.当我尝试上传3张大图片(每张图片约2 MB)时,该方法未触发.我几乎可以肯定,这是与某种文件最大总大小有关的问题.

I've also tried uploading multiple files separately. When I try to upload 3 large images (each image about 2 MB) the method did not fire. I'm almost certain that this is an issue related to some kind of total maximum file size.

<form method="post" action="/Test/Upload" enctype="multipart/form-data">
    <input type="file" name="image_1" />
    <input type="file" name="image_2" />
    <input type="file" name="image_3" />
    <input type="submit" value="Upload" />
</form>

推荐答案

问题是一个人可以上传的最大字节数.可以通过在web.config文件中设置maxRequestLength来更改.

Problem was with the maximum number of bytes that one can upload. This can be changed by setting the maxRequestLength in the web.config file.

参考

如何使用MVC 4上传大文件?

代码:

<system.web>

    <httpRuntime targetFramework="4.5" 
        maxRequestLength="2147483647"
        executionTimeout="1600" 
        requestLengthDiskThreshold="2147483647" />
   ....

</system.web>

链接问题的答案在<system.web>标记下列出了以下内容,但是我只能在<system.webServer>标记中添加它:

The answer for the linked question, lists the following under the <system.web> tag however I could only add it in the <system.webServer> tag:

<system.webServer>

    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="2147483647" />
        </requestFiltering>
    </security>
    ....

</system.webServer>

这篇关于上载多个文件-MVC ..是否有总文件大小限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆