错误的消息,文件已上传,但仍然是"超出最大请求长度"

时间:2015-07-03 09:08:32

标签: javascript c# jquery ajax jquery-file-upload

我在javascript中写了一个上传文件方法来上传大文件,它在blob中打开文件并重新连接服务器端的blob,这样我就可以上传大文件但是有问题......上传完成后我在服务器端接收文件(文件上传完全),它给了我Maximum request length exceeded

<!DOCTYPE HTML>
<html>
<head id="Head1" runat="server">
    <title>uploading file using jquery with generic handler ashx</title>
    <link id="Link2" rel="stylesheet" runat="server" media="screen" href="~/fileupload.css" />
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="http://malsup.github.com/jquery.form.js"></script>
    <%--<script src="JavaScript1.js" type="text/javascript"></script>--%>
    <script src="MyScript.js" type="text/javascript"></script>
</head>
<body>

    <form id="form1" runat="server" enctype="multipart/form-data">
        <div id="uploadFile">
            <div class="fileuploadDiv">
                <div class="status"></div>
                <input type="file" name="files[]" multiple="multiple" id="files" class="fileSelect" />
                <input type="submit" value="Upload" class="button" id="btnUpload" />
                <%--<div id="progressbar" class="progress"></div>--%>
                <div class="progress" id="progressbar">
                    <div class="bar" id="bar"></div>
                    <div class="percent" id="percent">0%</div>
                </div>
                <div id="messages"></div>
            </div>
        </div>
    </form>
</body>

MyScript.js

$(document).ready(function () {
    $("#btnUpload").click(function (evt) {
        var blobs = [];
        var fl = document.getElementById("files");
        var L = fl.files.length;
        var elem = document.getElementById("bar");
        var per = document.getElementById("percent");

        for (var i = 0; i < L ; i++) {
            var file = fl.files[i];
            var bytes_per_chunk = 3*1024*1024; //1048576
            var start = 0;
            var end = bytes_per_chunk;
            var size = file.size;
            var j = 1;
            while (start < size) {
                //push the fragments to an array
                blobs.push(file.slice(start, end));
                start = end;
                end = start + bytes_per_chunk;
            }
            while (blob = blobs.shift()) {
                var fileName = file.name;
                var fileType = file.type;
                var fileSize = file.size / 100;
                var rec = 0;
                rec = blob + rec;
                var xhr = new XMLHttpRequest();

                xhr.open('POST', 'Handler.ashx', false);

                xhr.onload = function () {
                    alert("in for");
                    elem.style.width = j + "%";
                    per.innerHTML = j + "%";
                    j++;
                    rec = 0;

                }

                xhr.setRequestHeader('X_FILE_NAME', fileName);
                xhr.setRequestHeader('Content-Type', fileType);
                xhr.send(blob);
            }
        }
    });
});

Handler.ashx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace test
{
    public class Handler : IHttpHandler
    {
        int fileCount = 0;
        public static void AppendAllBytes(string path, byte[] bytes)
        {
            //argument-checking here.
            try
            {
                using (var stream = new FileStream(path, FileMode.Append))
                {
                    stream.Write(bytes, 0, bytes.Length);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                byte[] buffer = new byte[context.Request.ContentLength];
                context.Request.InputStream.Read(buffer, 0, context.Request.ContentLength);
                string fileName = context.Request.Headers.Get(11);
                AppendAllBytes(context.Server.MapPath("~/upload/" + fileName), buffer);  
            }
            catch (Exception)
            {
                throw;
            }              
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我最后解决了

问题是&#34;提交&#34;我将其更改为按钮,因此在提交按钮后也不会发送整个文件

我这样做了:

input type="button" value="Upload" class="button" id="btnUpload" />

而不是

input type="submit" value="Upload" class="button" id="btnUpload" />