4
How to make a file available for download and viewing in browser?
For example, files of the type pdf and mp4.
When I want to make available to download the file I simply use the following routine:
public ActionResult Download(string fileName)
{
var path = Server.MapPath("~/Arquivos/Biblioteca/") + fileName;
return File(path, MimeMapping.GetMimeMapping(path), Path.GetFileName(path));
}
While trying to make available to be viewed in the browser I tried the following:
public ActionResult OpenFile(string fileName)
{
var path = Server.MapPath("~/Arquivos/Biblioteca/") + fileName;
return File(path, MimeMapping.GetMimeMapping(path));
}
The difference is that I did not inform the property referring to the file name for download.
On my computer (mode debug) the video and PDF are shown in the browser as expected.
But when trying to make available on the server, both are not opened and thus is made to download the file.
First I was trying to open the file directly, without passing a controller and an action, then time worked, not time, as it gave file error not found. Probably due to the size of the name.
For it to be possible I needed to register the mime on the server IIS (video/mp4).
But as I said, directly gives error and for the sake of good practices, I wish not to access the file directly.
So, how to make files available for viewing in the browser, when possible?