2017-10-10 42 views
0

我有一個input標籤,type="file"和一個使用Ajax將文件發送到Webapi端點的函數。

從html輸入發送文件到帶有Ajax的WebApi - 編碼問題

$('#myInput').change(function() { 
    if (this.files[0] === undefined) return; 
    sendToWebapi(this.files[0]); 
    this.value = null; 
}); 

function sendToWebapi(file) { 
    const data = new FormData(); 
    data.append('file', file); 
    $.ajax({ 
     url: "myWebApiPath", 
     contentType: "text/csv", 
     processData: false, 
     method: "POST", 
     data: data 
    }); 
} 

我的問題是,當發送到的WebAPI法國的字符不正確編碼。請參見下面的請求體的摘錄:

> ------WebKitFormBoundaryggBmtBMylhc9eoIE 
Content-Disposition: form-data; name="file"; filename="myfile.csv" 
Content-Type: application/vnd.ms-excel 

Date;Pi�ce;Journal;Libell�;D�bit;Lettrage;Cr�dit;Solde 
.... 

回答

0

嘗試明確設置字符集:

function sendToWebapi(file) { 
    const data = new FormData(); 
    data.append('file', file); 
    $.ajax({ 
     url: "myWebApiPath", 
     contentType: "text/csv;charset=ISO-8859-1", 
     processData: false, 
     method: "POST", 
     data: data 
    }); 
} 
+0

注意,同時增加字符集明確導致了我的請求主體被正確顯示,我的終點還是觀看字符串作爲前。 –

+0

@François你的意思是「我的端點仍然像以前一樣查看字符串」? – Oscar

+0

Myenpoint簽名是Task MyEndpointAsync([FromBody] List rows)。然後行中的每行都會看到像D bit這樣的東西。 –

相關問題