Reza Arani Reza Arani - 3 months ago 22
Javascript Question

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);

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";
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;
// database record update logic here ()

catch (Exception ac)

Eeverything seems to be fine, but the result is:

ContentLength: -2
ContentType: "image/png"
FileName: "icon-large.png"
InputStream: {System.Web.HttpInputStream}

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

I've found something new , it's working fine with 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" />

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?


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) 



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