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

时间:2013-02-22 06:07:34

标签: c# ajax html5 web-services

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

1 个答案:

答案 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]属性且没有添加静态修饰符。