I am uploading file with simple jquery ajax. it works fine in most of the cases but it fails sometime and shows 0 bytes file size on server. can any one help me why it is happening.. my ajax code is:
var formData = new FormData();
formData.append("emailId", "[email protected]");
formData.append("file1", $('#fileCtrlId')[0].files[0], $('#fileCtrlId')[0].files[0].name);
$.ajax({
async: true,
type: "POST",
url: "/fileupload.ashx",
data: formData,
processData: false,
contentType: false,
timeout: 300000, //5min
success: function (data) {
alert("success");
},
error: function (response) {
alert("Error");
}
},
failure: function (response) {
alert("failure");
}
}
});
My handler code to save file to disk is:
if (!string.IsNullOrEmpty(context.Request.Params["emailid"]))
{
var emailId = context.Request.Params["emailId"];
}
int fileLength = HttpContext.Current.Request.Files[0].ContentLength;
byte[] fileBytes = new byte[fileLength];
context.Request.Files[0].InputStream.Read(fileBytes, 0, fileLength);
using (var fileStream = new System.IO.FileStream("filepath", System.IO.FileMode.Create))
{
using (var buffer = new System.IO.BinaryWriter(fileStream))
{
buffer.Write(fileBytes);
buffer.Close();
}
fileStream.Close();
}
I have solved this problem. Actually the problem was on the server end where we were allowing http verbs methods. While creating the CORs policy we just use an array of strings of allowed methods like new
But unfortunately
IEdoesn't understand it and it takes comma separated method names.So, we have changed it to new
and it starts working.