我正在尝试使用formdata和html5将文件上传到aspx webservice。如果我没有在ajax调用中设置内容类型,则无法看到web服务。如果我将它设置为json,它会传入空数据。
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 :(得分:0)
使用Json Type
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]
属性且没有添加静态修饰符。