Home Core 2.0 MVC - How to link to a dynamic PDF saved on the file system
Reply: 0

Core 2.0 MVC - How to link to a dynamic PDF saved on the file system

user6573
1#
user6573 Published in September 21, 2018, 8:03 am

I have a .NET Core 2.0 MVC project. One of my models has a NotMapped IFormFile field called PDFUpload that takes an upload and saves the file to a place on the file system outside of the root, that path is saved in the PDF field. When users hit a specific URL, the PDF needs to be displayed in the browser or downloaded. My Details.cshtml View currently just shows the path

 @Html.DisplayFor(model => model.PDF)

How do I turn this into an actual link that will serve the file from a specific URL, and not show the path?

Update: Here is the working version

if (System.IO.File.Exists(pdf))
{
    var stream = new FileStream(pdf, FileMode.Open);
    // send the file to the browser
    stream.Position = 0;
    //return File(stream,"application/pdf","filename.pdf") //- will force download
    return new FileStreamResult(stream, "application/pdf");//will display in browser        
}
share|improve this question
  • 1
    In your controller don't use return view, instead user return File(model.PDF, "application/pdf"); to display the PDF in browser or return File(model.PDF, "application/pdf", "your file name.pdf"); to force download – mylee Feb 14 at 1:51
  • mylee THANK YOU SO MUCH for that tip! – user6383418 Feb 14 at 6:04

1 Answer 1

active oldest votes
up vote 1 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.315102 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO