3

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?

Reza Arani
  • 66
  • 1
  • 5
  • have tried to print data between: jQuery.each($()) and $.ajax({}) – Destrif Jul 07 '16 at 12:05
  • Here is my Request :POST http://localhost/FileUploadHandler.ashx HTTP/1.1 Connection: keep-alive Content-Length: 338630 Pragma: no-cache Cache-Control: no-cache Accept: */* Origin: http://localhost X-Requested-With: XMLHttpRequest Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryMg4rlBFcHS4txlHr Referer: http://localhost/car-information.html Accept-Encoding: gzip, deflate Accept-Language: en-US,en;q=0.8,fa;q=0.6 ------WebKitFormBoundaryMg4rlBFcHS4txlHr Content-Disposition: form-data; name="file-0"; filename="icon-large.png" Content-Type: image/png – Reza Arani Jul 07 '16 at 12:14
  • and also my Content-Length is 338630 . I used fiddler to check my request – Reza Arani Jul 07 '16 at 12:15
  • It may help http://stackoverflow.com/questions/26684689/request-files-is-always-null – Destrif Jul 07 '16 at 12:24
  • It seems it's not the solution , but after changing contentType to 'json' the context.Request.ContentLength is 338630 but context.Request.Files is empty : {System.Web.HttpFileCollection} base {System.Collections.Specialized.NameObjectCollectionBase}: {System.Web.HttpFileCollection} AllKeys: {string[0]} – Reza Arani Jul 07 '16 at 12:31
  • 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 – Reza Arani Jul 07 '16 at 12:54

1 Answers1

0

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?

Reza Arani
  • 66
  • 1
  • 5