Implementing a FileStreamResult
from C# in a SPA website (using .NET Core 2, SPA React template), I make a request to fetch a file from my endpoint. This triggers the following response in C#:
var file = await _docService.GetFileAsync(token.UserName, instCode.Trim()
.ToUpper(), fileSeqNo);
string contentType = MimeUtility.GetMimeMapping(file.FileName);
var result = new FileStreamResult(file.File, contentType);
var contentDisposition = new ContentDispositionHeaderValue("attachment");
Response.Headers[HeaderNames.ContentDisposition] =
contentDisposition.ToString();
return result;
The response received is then processed using msSaveBlob
(specifically for MS, although issues arise even when using createObjectURL
and a different browser. Despite trying various solutions, none seem to work). Below is the code snippet used to send the request and receive the PDF FileStreamResult
from the server.
if (window.navigator.msSaveBlob) {
axios.get(url).then(response => {
window.navigator.msSaveOrOpenBlob(
new Blob([response.data], {type: "application/pdf"}),
filename);
});
The issue arises when the PDF file received has incorrect encoding, causing it not to open properly.
Attempts to rectify this include adding encoding to the end of type:
{type: "application/pdf; encoding=UTF-8"}
as suggested in various forums, but without success.
By comparing the PDF file fetched using a different method, it is evident that the encoding is incorrect. Special characters are not displayed correctly, despite the response header indicating that the PDF file should be in UTF-8
. Unfortunately, I am unsure how to verify and rectify this.