Angular 如何从服务器响应文件2中读取内容处置头
在angular 2文档中,我找不到如何从内容配置中读取文件名。有人可以在angular 2中从服务器读取标题吗?您可以在这里尝试这种代码 注意:不要使用地图Angular 如何从服务器响应文件2中读取内容处置头,angular,typescript,Angular,Typescript,在angular 2文档中,我找不到如何从内容配置中读取文件名。有人可以在angular 2中从服务器读取标题吗?您可以在这里尝试这种代码 注意:不要使用地图 this.http.post(URL, DATA) .subscribe((res) => { var headers = res.headers; console.log(headers); //<--- Check log for content disposition var conte
this.http.post(URL, DATA)
.subscribe((res) => {
var headers = res.headers;
console.log(headers); //<--- Check log for content disposition
var contentDisposition= headers.get('content-disposition');
});
this.http.post(URL,数据)
.订阅((res)=>{
var headers=res.headers;
console.log(headers);//对于那些抱怨最佳解决方案不起作用,并且只有内容类型在头中的人,您需要设置“访问控制公开头”:“内容处置”在服务器端。我正在使用asp.net core,然后我必须执行以下操作
app.UseCors(builder =>
builder.WithOrigins(originsAllowed.ToArray())
.AllowAnyMethod()
.AllowAnyHeader()
.WithExposedHeaders("Content-Disposition")
使用新的AngularHTTP,可以在服务代码中尝试以下操作
downloadLink(): Observable<HttpResponse<Blob>> {
return this.http.get<Blob>(this.someURL, {
observe: 'response',
responseType: 'blob' as 'json'
});
}
在Angular 7中,向上投票的方法不起作用,您需要这样做:
const responseHeaderFilename = response.headers.get('content-disposition');
有关更多详细信息,请查看官方文件:
还要确保“内容配置”如@mslugx answer中所示公开,在angular中,我们可以读取文件名,如下所示
this.http.post(URL, DATA).subscribe(
(res) => {
var contentDisposition = res.headers.get('content-disposition');
var filename = contentDisposition.split(';')[1].split('filename')[1].split('=')[1].trim();
console.log(filename);
});
但最主要的是我们需要在API中指定访问控制暴露头,如下所示
注意:最后一行是必填的
FileInfo file = new FileInfo(FILEPATH);
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = file.Name
};
response.Content.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
带有角度9和表达式
需要在Express中允许此标题
res.setHeader('Access-Control-Expose-Headers', 'Content-Disposition');
棱角的
this.http.get(url, { observe: 'response', responseType: 'blob' as 'json' })
.subscribe((res: any) => {
console.log(res.headers.get('content-disposition'));
});
从响应头获取文件名
(data)=>{ //the 'data' is response of file data with responseType: ResponseContentType.Blob.
let filename = data.headers.get('content-disposition').split(';')[1].split('=')[1].replace(/\"/g, '')
}
感谢@Partha Sarathi Ghosh的精彩回复,它似乎也在使用map Method,如何使用Promise
达到同样的效果?对我不起作用。只有内容类型在标题中不适用。只有内容类型在标题中感谢Nehal!这对我有帮助。我没有公开标题作为回应。添加观察:“response”
使我的zip文件损坏添加响应类型也会损坏服务器端添加的数据,这是我在头键中得到的[“缓存控制”、“内容长度”、“内容类型”、“过期”、“pragma”]
但在浏览器中访问控制暴露标题:内容处置和内容处置:abc.xml
出现在我的响应标题for.NET Coreapp.Use((ctx,next)=>{ctx.response.HEADERS.Add(“访问控制暴露标题”,“*”)中;
请注意,上面的代码缺少引号的替换项,如果文件名中有自定义字符,则会出现引号。您应该在.trim()之后添加.replace(/\“/g”“)。这仅当文件名是处置字符串中的[1]元素时才有效。for.NET Coreapp.Use((ctx,next)=>{ctx.Response.Headers.Add(“Access Control Expose Headers”,“*””);
仅当文件名是处置字符串中的[1]元素时,此操作才有效。
this.http.get(url, { observe: 'response', responseType: 'blob' as 'json' })
.subscribe((res: any) => {
console.log(res.headers.get('content-disposition'));
});
(data)=>{ //the 'data' is response of file data with responseType: ResponseContentType.Blob.
let filename = data.headers.get('content-disposition').split(';')[1].split('=')[1].replace(/\"/g, '')
}