Click here to Skip to main content
14,640,651 members
Rate this:
Please Sign up or sign in to vote.
I have wrote an upload file method in javascript to upload big files it slpits thefiles in blobs and reattach the blobs in server side again so I can upload big files but there is a problem... after the upload is done and I receive the file in server side (the fileuploads completely), it gives me this `Maximum request length exceeded`
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="test.Default" %>

<!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>
</html>



MyScript.js

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

        for (var i = 0; i &amp;lt; 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 &amp;lt; 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(&amp;#39;POST&amp;#39;, &amp;#39;Handler.ashx&amp;#39;, false);

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

                }

                xhr.setRequestHeader(&amp;#39;X_FILE_NAME&amp;#39;, fileName);
                xhr.setRequestHeader(&amp;#39;Content-Type&amp;#39;, 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(&amp;amp;quot;~/upload/&amp;amp;quot; + fileName), buffer);
                }
                catch (Exception)
                {
                    throw;
                }
            }

            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 1

You need to set maxAllowedContentLength and maxRequestLength in web.config.

Refer - Maximum request length exceeded[^]
Comments
Lalyka 3-Jul-15 5:30am
   
but the problem is that,I don't have access to server config file and this is a test I am doing on my computer, for this reason I have used the slicing method.
the file is uploaded successfully but after uploading the file, it gives this error also. in fact it should not gives the error because the code is working fine with uploading
   
Where is your server? Where you application is deployed?
Lalyka 3-Jul-15 5:59am
   
it's a dedicated of a big companybut anyway Idcan not ask them to change the configurations
   
You need to ask them. You have to increase the length in web.config to allow large files.
Lalyka 3-Jul-15 6:10am
   
but the code works, I can upload, this erro is wrong. because it uploads the file successfully
   
Have you opened those files from server? Do they open perfectly?
Rate this:
Please Sign up or sign in to vote.

Solution 2

I Solved it Finally

the problem is the "Submit" that I changed it to button so itdoes not send the whole file also after submitting the button

I did this :

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


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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100