繁体   English   中英

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

[英]How to set the maxAllowedContentLength upper 4,294,967,295 bytes while running on IIS7?

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

您可以尝试在客户端自动切片大型文件,而不是在服务器端合并大型文件。 这是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);
        }
    }

在下面添加到您的web.config文件中,

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM