[英]How to read the content-disposition in Web API?
I use Asp.Net API (.Net 5) in my backend.我在后端使用 Asp.Net API (.Net 5)。 I use blazor wasm in my frontend.
我在前端使用 blazor wasm。
A method of my controller is:我的controller的一个方法是:
public FileContentResult GetExport()
{
var IndividualBl = new IndividualBl();
return ResultBlob(IndividualBl.GetExport(), "individual-export.xlsx");
}
protected FileContentResult ResultBlob(byte[] byteArray, string fileName)
{
return File(byteArray, "application/octet-stream", fileName);
}
When I use Swagger, I download the file with the name included in the header. My Response Headers:当我使用 Swagger 时,我下载了名称包含在 header 中的文件。我的响应标头:
access-control-allow-origin: *
content-disposition: attachment; filename=individual-export.xlsx; filename*=UTF-8''individual-export.xlsx
content-length: 4218
content-type: application/octet-stream
date: Mon, 23 Nov 2020 14:39:34 GMT
server: Microsoft-IIS/10.0
status: 200
x-powered-by: ASP.NET
Now I try to get this file by service:现在我尝试通过服务获取此文件:
public async Task<StreamFileModel> GetExport()
{
var url = $"{_httpClient.BaseAddress}{IndividualUrls.IndividualController}/{IndividualUrls.GetExport}";
var result = await _httpClient.GetAsync(url);
var header = result.Content.Headers.ContentDisposition;
var content = await result.Content.ReadAsByteArrayAsync();
return new StreamFileModel
{
File = content,
Name = header.FileName
};
}
My content is OK but the header is null (= ContentDisposition is null)我的内容没问题,但 header 是 null(= ContentDisposition 为空)
It is not the good method to get the content-disposition value?这不是获取内容处置值的好方法吗?
Thanks谢谢
In API project, you need to use Cors policy to allow content-disposition in header:在 API 项目中,需要使用 Cors 策略允许 header 中的内容处置:
app.UseCors(x => x.WithExposedHeaders("content-disposition"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.