[英]Not able to change Content-Type in xmlHttpRequest
我試圖在發送xhr數據之前設置Content-Type,如下所示
function uploadFile() {
var files = document.getElementById("file1") .files[0] ;
var formdata = new FormData();
formdata.append("Key", files);
ajax = new XMLHttpRequest();
ajax.upload.addEventListener("progress", progressHandler, false);
ajax.addEventListener("load", completeHandler, false);
ajax.addEventListener("error", errorHandler, false);
ajax.addEventListener("abort", abortHandler, false);
ajax.open("POST", "./Save");
ajax.setRequestHeader('Content-Type','multipart/form-data;');
ajax.send(formdata);
}
通過更改內容類型,我無法在服務器端獲取數據。
如果我刪除用於設置內容類型的代碼,則其工作正常
我的服務器端代碼如下
HttpContext.Current.Request.Files["Key"]
有建議嗎?
關鍵是Content-type: multipart/form-data
后面應加上boundary: (your file boundary)
但是因為您明確設置了它,所以它不存在
Content-type: multipart/form-data; boundary=----WebKitFormBoundaryrKBH6bAMJIdepLCI
如果您未設置Content-type
那么XHR足夠聰明,可以理解您正在發送文件,因此,我建議您不要設置它或設置boundary
(在這里查看fetch-多部分/表單數據POST中缺少邊界 )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.