2013-02-22 67 views
0

我正嘗試使用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

回答

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]屬性和方法聲明不存在static修飾符。