0

我正在尝试使用 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)
        {
...
}
4

1 回答 1

0

使用 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]在方法声明中添加了属性并且没有静态修饰符。

于 2013-02-22T06:18:32.143 回答