I have a web api that need to let the user download/inline a binary file I have saved to a database.
So far now I could achieve it like this:
string ContentType = (string) (result["content_type"] ?? "");
string FileName = (string) (result["file_name"] ?? "");
byte[] content = (byte[]) result["data"];
Stream stream = new MemoryStream(content);
var fullFileName = Path.GetTempFileName();
File.WriteAllBytes(fullFileName, content);
_res.ContentType = ContentType;
_res.StatusCode = (int)HttpStatusCode.OK;
_res.Headers.Add("Content-Disposition", $"{Disposition}; filename=\"{FileName}\"");
await _res.SendFileAsync(fullFileName);
I'd like to know how could I send directly the memory stream to the response, in order to avoid saving the file to disk.