Search code examples
javascriptjqueryasp.netajaxashx

HttpPostedFile.ContentLength is always -2 after uploading files through jQuery AJAX


I have strange problem with jQuery AJAX and ASHX behavior. Here is my code:

<input type="file"  ID="FileUpload1"  multiple="multiple" />
<input type="button" ID="Button1"  Text="Upload Selected File(s)" />
function Upload() {
    var data = new FormData();
    jQuery.each($('#FileUpload1')[0].files, function (i, file) {
        data.append('file-' + i, file);
    });

    $.ajax({
        url: "/basic/fileupload/FileUploadHandler.ashx",
        type: "POST",
        contentType: false,
        processData: false,
        cache: false,
        async: true,
        data: data,
        error: function (data) {
            alert("Erro no envio de fotos do projecto. " + data.status);
        }
    });
}
public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/plain";
    try
    {
        string dirFullPath = HttpContext.Current.Server.MapPath("~/MediaUploader/");
        string[] files;
        int numFiles;
        files = System.IO.Directory.GetFiles(dirFullPath);
        numFiles = files.Length;
        numFiles = numFiles + 1;
        string str_image = "";

        foreach (string s in context.Request.Files)
        {
            HttpPostedFile file = context.Request.Files[s];
            string fileName = file.FileName;
            string fileExtension = file.ContentType;

            if (!string.IsNullOrEmpty(fileName))
            {
                fileExtension = System.IO.Path.GetExtension(fileName);
                str_image = "MyPHOTO_" + numFiles.ToString() + fileExtension;
                string pathToSave_100 = HttpContext.Current.Server.MapPath("~/files/") + str_image;
                file.SaveAs(pathToSave_100);
            }
        }
        //  database record update logic here  ()

        context.Response.Write(str_image);
    }
    catch (Exception ac)
    {
    }
}

Eeverything seems to be fine, but the result is:

context.Request.Files[0]
{System.Web.HttpPostedFile}
    ContentLength: -2
    ContentType: "image/png"
    FileName: "icon-large.png"
    InputStream: {System.Web.HttpInputStream}

I can receive file name but the ContentLength is always -2 and after saving the file It's just 0 bytes is size. Could you please help me to solve this problem?

UPDATE : I've found something new , it's working fine with ASP.net Development Server (Running Application Directly by pushing F5 Key in Visual Studio) but something is wrong with IIS 8.5 configuration

also My web.config request length parameters are :

    <httpRuntime requestValidationMode="2.0" maxRequestLength="10240000" requestLengthDiskThreshold="10240000" />

UPDATE 2: changing Application pool's to Managed pipeline mode to Classic will solve the problem , but I will loose my URL Rewriting, so I can't change my Pipeline Mode. Any Idea?


Solution

  • I've found the solution to solve this problem. I don't know is it fine or not but solved my case :

    I used

    void Application_BeginRequest(object sender, EventArgs e) 
    

    in

    global.asax

    file to manage request because the contents is visible there correctly. Any other Idea?