I feel like I'm so close to getting this right.
In this example the file is 125 KB and of type image/png.
I have a C# controller method that constructs a HttpResponseMessage with a file as follows:
public async Task<HttpResponseMessage> Get(int id)
{
var file = await _api.GetFileAsync(id);
var httpResult = new HttpResponseMessage(HttpStatusCode.OK);
httpResult.Content = new ByteArrayContent(file.Bytes);
httpResult.Content.Headers.ContentType = new MediaTypeHeaderValue(file.ContentType);
httpResult.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = file.Name
}; //attachment will force download
return httpResult;
}
The raw file itself is kept in the byte[] of file.Bytes
.
And here's a JavaScript function that calls it:
get(fileId, fileName) {
return this.httpFetch.fetch(`${this.apiRoot}/v1/files/${fileId}`)
.then(response => response.blob())
.then(data => {
if (window.navigator.msSaveOrOpenBlob)
window.navigator.msSaveBlob(data);
else {
var link = document.createElement("a");
link.href = window.URL.createObjectURL(data);
link.download = fileName;
document.body.appendChild(link); // Required for FF
link.click();
}
});
}
Here's a preview of the payload according to Chrome network tools. Seems kind of empty to me...
The type and filename are correct and it does auto-download, but I end up with a file that's 336 bytes and unreadable.
What am I missing here?
More Info: httpFetch
is a wrapper of fetch
by Aurelia (aurelia-fetch-client). I'm using Aurelia as my front-end framework.