使用ajax和jquery上传文件会返回SOME文件的网络错误 [英] Upload files using ajax and jquery is returning network error for SOME files

查看:63
本文介绍了使用ajax和jquery上传文件会返回SOME文件的网络错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jquery和ajax上传文件完成了一个完整的javascript函数......一切都工作得很好,直到我意识到对于某些文件,错误函数是在ajax而不是成功函数中触发的。以下是我的代码:



 $。ajax({
url:'MyService.asmx / UploadFiles',
类型:POST,
contentType:false,
processData:false,
data:fileData,//表单包含文件和一些数据的数据
dataType:text,
成功:函数(响应){
...
}
错误:函数(jqXHR,异常){
//alert(jqXHR.status);
var msg ='';
if(jqXHR.status === 0){
msg ='Not connect.\\\
Verify Network。';
} else if( jqXHR.status == 404){
msg ='未找到请求的页面。[404]';
}否则if(jqXHR.status == 500){
msg ='内部服务器错误[500]。';
}否则if(例外==='parsererror'){
msg ='R等于JSON解析失败。';
}否则如果(异常==='超时'){
msg ='超时错误。';
} else if(exception ==='abort'){
msg ='Ajax request aborted。';
} else {
msg ='Uncaught Error.\'+ jqXHR.responseText;
}
alert(msg);
}
});





抛出的错误是Not connect.\ Verify Network。我搜索了这个错误,我发现当服务无法访问或跨站点脚本(访问被拒绝)时抛出它...奇怪的是有些文件上传成功,有些文件没有上传,这意味着服务是无法访问可访问和访问...有关文件类型,它们都是最大大小为5 MB的文档文件,每当我删除无法上传的文件中的所有内容时,我再次尝试上传:功能成功...

我在onchange事件中触发上传功能:

< input type =fileclass =HideFileonchange =UploadFilesnew( ); onclick =resetInput(this)id =UploadFilenew/> ; 



我的输入是在.aspx页面的asp:UpdatePanel内部,所以没有表格和动作...

为什么有些文件是上传成功,其他人不是?如何解决我的问题以便能够上传所有文件?



我尝试了什么:



我试图在某些解决方案中建议更改false:

< input type =fileclass = HideFileonchange =UploadFilesnew(); return false; onclick =resetInput(this)id =UploadFilenew/> ; 





但它不起作用



我还安装了最新的jquery版本,因为我有一个旧的,但没有工作

解决方案

.ajax({
url:'MyService.asmx / UploadFiles',
类型:POST,
contentType:false,
processData:false,
data:fileData,//表单包含文件和一些数据的数据
dataType:text ,
成功:函数(响应){
...
}
错误:函数(jqXHR,异常){
//alert(jqXHR.status) ;
var msg ='';
if(jqXHR.status === 0){
msg ='Not connect.\\\
Verify Network。';
} else if(jqXHR.status == 404){
msg ='未找到请求的页面。[404]';
}否则if(jqXHR.status == 500){
msg ='内部服务器错误[500]。';
} else if(exception ==='parsererror'){
msg ='请求的JSON解析失败。';
} else if(exception ==='timeout'){
msg ='超时错误。';
} else if(exception ==='abort'){
msg ='Ajax request aborted。';
} else {
msg ='Uncaught Error.\'+ jqXHR.responseText;
}
alert(msg);
}
});





抛出的错误是Not connect.\ Verify Network。我搜索了这个错误,我发现当服务无法访问或跨站点脚本(访问被拒绝)时抛出它...奇怪的是有些文件上传成功,有些文件没有上传,这意味着服务是无法访问可访问和访问...有关文件类型,它们都是最大大小为5 MB的文档文件,每当我删除无法上传的文件中的所有内容时,我再次尝试上传:功能成功...

我在onchange事件中触发上传功能:

< input type =fileclass =HideFileonchange =UploadFilesnew( ); onclick =resetInput(this)id =UploadFilenew/> ; 



我的输入是在.aspx页面的asp:UpdatePanel内部,所以没有表格和动作...

为什么有些文件是上传成功,其他人不是?如何解决我的问题以便能够上传所有文件?



我尝试了什么:



我试图在某些解决方案中建议更改false:

< input type =fileclass = HideFileonchange =UploadFilesnew(); return false; onclick =resetInput(this)id =UploadFilenew/> ; 





但它不起作用



我还安装了最新的jquery版本,因为我有一个旧的,但没有工作


我发现它:

我刚刚在我的web.config文件中添加了这个块

< system.web> 
< httpRuntime maxRequestLength =1048576/>
< /system.web>





问题是当文件大小最接近5MB时,请求失败


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...
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

解决方案

.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...
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


I found it:
I have just added this block in my web.config file

<system.web>
     <httpRuntime maxRequestLength="1048576" />
</system.web>



The problem was when file size is nearest 5MB, then the request fails


这篇关于使用ajax和jquery上传文件会返回SOME文件的网络错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆