繁体   English   中英

内容处置始终为 null

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM