简体   繁体   中英

How to read the content-disposition in Web API?

I use Asp.Net API (.Net 5) in my backend. I use blazor wasm in my frontend.

A method of my controller is:

    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:

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)

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:

app.UseCors(x => x.WithExposedHeaders("content-disposition"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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