如何在IIS7上运行时设置maxAllowedContentLength上限4,294,967,295字节?

时间:2016-03-11 13:22:15

标签: asp.net iis-7

根据MSDN,maxAllowedContentLength最大值为 4,294,967,295字节。 是否可以上传更大的文件?

2 个答案:

答案 0 :(得分:1)

您可以尝试在客户端自动切片大文件,而不是在服务器端合并它。以下是mvc的示例代码:http://forums.asp.net/t/1742612.aspx?How+to+upload+a+big+file+in+Mvc+

客户方:

    var uploadComplete = function () {
        var formData = new FormData();
        formData.append('fileName', file.name);
        formData.append('completed', true);

        var xhr2 = new XMLHttpRequest();
        xhr2.open("POST", "/Files/UploadComplete", true); //combine the chunks together
        xhr2.send(formData);
    }

    function upload(file) {
        var blob = file;
        var BYTES_PER_CHUNK = 77570; // sample chunk sizes.
        var SIZE = blob.size;

        //upload content
        var start = 0;
        var end = BYTES_PER_CHUNK;
        var completed = 0;
        var count = SIZE % BYTES_PER_CHUNK == 0 ? SIZE / BYTES_PER_CHUNK : Math.floor(SIZE / BYTES_PER_CHUNK) + 1;

        while (start < SIZE) {
            var chunk = blob.webkitSlice(start, end);
            var xhr = new XMLHttpRequest();
            xhr.onload = function () {
                completed = completed + 1;
                if (completed === count) {
                    uploadComplete();
                }
            };
            xhr.open("POST", "/Files/MultiUpload", true);
            xhr.send(chunk);

            start = end;
            end = start + BYTES_PER_CHUNK;
        }
    }

服务器端:

    [HttpPost]
    public string MultiUpload()
    {
        var chunks = Request.InputStream;

        string path = Server.MapPath("~/App_Data/Uploads/Tamp");
        string newpath = Path.Combine(path, Path.GetRandomFileName());

        using (System.IO.FileStream fs = System.IO.File.Create(newpath))
        {
            byte[] bytes = new byte[77570];

            int bytesRead;
            while ((bytesRead = Request.InputStream.Read(bytes, 0, bytes.Length)) > 0)
            {
                fs.Write(bytes, 0, bytesRead);
            }
        }
        return "test";
    }

    [HttpPost]
    public string UploadComplete(string fileName, bool completed)
    {
        if (completed)
        {
            string path = Server.MapPath("~/App_Data/Uploads/Tamp");
            string newpath = Path.Combine(path, fileName);
            string[] filePaths = Directory.GetFiles(path);

            foreach (string item in filePaths)
            {
                MergeFiles(newpath, item);
            }
        }
        return "success";
    }

    private static void MergeFiles(string file1, string file2)
    {
        FileStream fs1 = null;
        FileStream fs2 = null;
        try
        {
            fs1 = System.IO.File.Open(file1, FileMode.Append);
            fs2 = System.IO.File.Open(file2, FileMode.Open);
            byte[] fs2Content = new byte[fs2.Length];
            fs2.Read(fs2Content, 0, (int)fs2.Length);
            fs1.Write(fs2Content, 0, (int)fs2.Length);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message + " : " + ex.StackTrace);
        }
        finally
        {
            fs1.Close();
            fs2.Close();
            System.IO.File.Delete(file2);
        }
    }

答案 1 :(得分:0)

在web.config文件中添加以下内容,

<configuration>
    <system.web>
        <httpRuntime maxRequestLength="1048576" />
    </system.web>
</configuration>