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,
dataType: "text",
success: function (response) {
...
}
error: function (jqXHR, exception) {
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...
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 ...
So why some files are uploaded successfully and others are not? How can I resolve my problem in order to be able to upload all files?
What I have tried:
I have tried to return false on change as it was suggested in some solutions:
<input type="file" class="HideFile" onchange="UploadFilesnew();return false;" onclick="resetInput(this)" id="UploadFilenew" /> ;
But it did not work
I have also installed the latest jquery version as I had an old one, but did not work