[英]Content-Disposition is always null
我的 web api 2.0 中有一個 function 可以下載文件,但有一段時間沒有嘗試,直到昨天才發現它不再有效。 我已經部分解決了createObjectURL
的問題,但我注意到的一件事是,雖然在我的 web api 中設置了Content-Disposition
:
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
var response = new HttpResponseMessage();
var filename = this.Document.GetFilename();
var mimeType = MimeMapping.GetMimeMapping(filename);
response.Content = new StreamContent(new MemoryStream(this.Document.ToData()));
response.Content.Headers.ContentLength = this.Document.Data.Length;
response.Content.Headers.ContentType = new MediaTypeHeaderValue(mimeType);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = filename
};
return Task.FromResult(response);
}
然而,當我在JavaScript
中檢查它時,它總是來自響應 header 的 null:
success: function (blob, status, xhr) {
var filename = "";
var disposition = xhr.getResponseHeader('Content-Disposition');
...
}
任何想法為什么?
謝謝。
更新 1:
當我在瀏覽器的“ Network
”部分檢查響應時,但當我調用xhr.getAllResponseHeaders()
或xhr.getResponseHeader('Content-Disposition');
,如您在下面的快照中所見,function 調用都不會返回它:
我剛剛發現了問題,不幸的是我自己做的!
我的ajax請求中有以下內容:
xhrFields: {
responseType: 'blob'
}
但后來我介紹了憑據支持並介紹了這個
xhrFields: {
withCredentials: true
}
但是直到今天早上我才發現這一點,當我意識到我愚蠢地聲明如上,即 2 個語句,因此,當我應該將其聲明為時,覆蓋了responseType: 'blob'
設置:
xhrFields: {
responseType: 'blob',
withCredentials: true
}
從服務器端,請將此 header 設置為響應"Access-Control-Expose-Headers”
,值為“*”
,以使前端腳本讀取響應中的所有標頭。或者只為這種情況設置“Content-Disposition”
,而不是* 的,只允許前端腳本讀取 content-disposition header 的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.