C# Web服务方法名称无效(通过ajax w/html5 formdata将文件上载到aspx webservice)
我正在尝试使用formdata和html5将文件上载到aspx Web服务。如果我没有在ajax调用中设置内容类型,它就看不到Web服务。如果我将其设置为json,它将传入空数据C# Web服务方法名称无效(通过ajax w/html5 formdata将文件上载到aspx webservice),c#,ajax,html,web-services,C#,Ajax,Html,Web Services,我正在尝试使用formdata和html5将文件上载到aspx Web服务。如果我没有在ajax调用中设置内容类型,它就看不到Web服务。如果我将其设置为json,它将传入空数据 var formData = new FormData(); file = $("#fileToUpload")[0].files[0]; formData.append("file", file); $.ajax({ url: 'http://localhost:50
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)
{
...
}
var formData=new formData();
file=$(“#fileToUpload”)[0]。文件[0];
formData.append(“文件”,file);
$.ajax({
网址:'http://localhost:50101/xxxxx.asmx/UploadFile“,//用于处理数据的服务器脚本
键入:“POST”,
成功:函数(msg){
log(“error |”+JSON.stringify(msg));
},
错误:函数(msg){
log(“Success |”+JSON.stringify(msg));
},
数据:formData,
//数据:{'+JSON.stringify(formdata)+'},
cache:false,
contentType:false,
processData:false
});
});
//…网络服务。。。。
公共字符串上载文件(对象文件流)
{
...
}
使用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]
属性和静态修饰符。您想以json的形式发送文件吗???不太可能,我看到的一些示例就是这么做的……对我来说没有多大意义。