Web服务方法名称无效(通过ajax w/html5 formdata将文件上传到aspx web服务)

问题描述:

我正尝试使用formdata和html5将文件上传到aspx web服务。如果我没有在ajax调用中设置内容类型,它将看不到web服务。如果我将它设置为json,它会传入空数据。Web服务方法名称无效(通过ajax w/html5 formdata将文件上传到aspx web服务)

var formData = new FormData(); 
    file = $("#fileToUpload")[0].files[0]; 
    formData.append("file", file); 

    $.ajax({ 
     url: 'http://localhost:50101/xxxxx.asmx/UploadFile', //server script to process data 
     type: 'POST', 
     success: function(msg) {  
      console.log("error | " + JSON.stringify(msg)); 
     }, 
     error: function(msg) { 
      console.log("Success | " + JSON.stringify(msg));  
     }, 
     data: formData, 
     //data: {' + JSON.stringify(formdata)+'}, 
     cache: false, 
     contentType: false, 
     processData: false 
    }); 
}); 


<form enctype="multipart/form-data"> 
    <input id="fileToUpload" type="file" /> 
    <input type="button" value="Upload" /> 
</form> 



//....webservice.... 
    public String UploadFile(Object fileStreams) 
     { 
... 
} 
+0

你想发送一个文件为json ??? – Musa 2013-02-22 06:12:41

+0

不是,这就是我看到说过的几个例子......对我来说没有什么意义。 – 2013-02-22 16:14:28

使用JSON类型

jQuery.ajax({ 
     type: "POST", // or GET 
     url: "http://localhost:50101/xxxxx.asmx/UploadFile", 
     data: formdata, 
     contentType: "application/json; charset=utf-8", 
     dataType: "json" 
     success: function(msg) {  
     console.log("error | " + JSON.stringify(msg)); 
     }, 
     error: function(msg) { 
     console.log("Success | " + JSON.stringify(msg)); 
     } 
    }); 

,并检查是否添加[webmethod]属性和方法声明不存在static修饰符。