Skip to content

Instantly share code, notes, and snippets.

@csharpforevermore
Created September 16, 2020 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csharpforevermore/50196adca280283e782b43b86502ab81 to your computer and use it in GitHub Desktop.
Save csharpforevermore/50196adca280283e782b43b86502ab81 to your computer and use it in GitHub Desktop.
Return a PDF as a file from a web API service
[HttpGet]
public FileStreamResult Download()
{
var stream = System.IO.File.OpenRead(AppSettings.PdfFileName);
return new FileStreamResult(stream, AppSettings.MimeType);
}
[HttpGet("open-pdf")]
public IActionResult Get()
{
FileStream stream = GetFileBytesById(AppSettings.PdfFileName);
if (stream == null)
return NotFound();
return File(stream, AppSettings.MimeType);
}
[HttpGet("download/{name}")]
public FileStreamResult DownloadByNamec(string name)
{
var stream = GetFileBytesById(AppSettings.PdfFileName);
return new FileStreamResult(stream, AppSettings.MimeType)
{
FileDownloadName = AppSettings.PdfFileName
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment