I made a complete javascript function using jquery and ajax to upload files ... Everything is working perfectly untill I realized that for some files the error function is fired in ajax instead of success function. Below is my code:
$.ajax({
url: 'MyService.asmx/UploadFiles',
type: "POST",
contentType: false,
processData: false,
data: fileData, // form data that contains file and some data
dataType: "text",
success: function (response) {
...
}
error: function (jqXHR, exception) {
//alert(jqXHR.status);
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connect.\n Verify Network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + jqXHR.responseText;
}
alert(msg);
}
});
The error thrown is "Not connect.\n Verify Network". I have searched for this error I found that it is thrown when service is not reachable or when cross-site scripting (access is denied) ... The weird thing is that some files are uploaded successfully, others are not, which means service is reachable and access is not denied ... And concerning file types, they are all docs files with max size of 5 MB, whenever I delete everything inside the file that could not be uploaded and I try again to upload: function succeeds... So why some file are uploaded successfully and others are not? How can I resolve my problem in order to be able to upload all files?
Update
I am firing the upload function in onchange event:
<input type="file" class="HideFile" onchange="UploadFilesnew();" onclick="resetInput(this)" id="UploadFilenew" />
;
My input is inside an asp:UpdatePanel in a .aspx page, so no forms and actions
Please add the following code in web.config. And try again. I think sometimes the request fails due to the size of the file.
<system.web>
<httpRuntime maxRequestLength="1048576" />
</system.web>