I created an MVC endpoint returning a file and then deleting it on the server side once the client download is finished. My implementation is based on this SO post, with this difference that my endpoint serves plain text files.
[Authorize, HttpGet]
public FileResult DownloadLogFile(string fileName)
{
var path = Path.Combine(this.logFilesRoot, fileName);
var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None, 4096, FileOptions.DeleteOnClose);
return File(fs, MediaTypeNames.Text.Plain, fileName);
}
This code works in most of the cases, but for bigger files (more than 40 MB) it randomly fails from time to time:
The picture shows my Chrome with an error, but I checked it also with Firefox and Edge, so it's not a problem with browser.
What could be the reason for the random failures?
Some details:
- Net7
- ASPNetCore 6
- Windows OS
EDIT
I've changed the endpoint's method to "POST", but still no good. I'm getting net::ERR_CONNECTION_RESET 200
exceptions from JS fetch
method now.
The code I use on client side is:
const redirectToLogFile = (fileName: SignalR.IDownloadableLogFileDetails): void => {
const url = `${Utils.root}home/DownloadLogFile/${fileName.FileName}`;
fetch(url, { method: "POST" })
.then(response => response.blob())
.then(blob => {
const a = document.createElement("a");
a.href = window.URL.createObjectURL(blob);
a.download = fileName;
a.click();
})
.catch(err => console.log(err));
}