How to serve a file in MVC without setting a URL?

I have a tree viewer that allows the user to view files and subdirectories, when the user reaches the file, the website will go to https://website.com/path/subpath/file.pdf . Assuming that I can determine that the user is viewing the file, the following will happen:

  • The controller will generate a SAS key to retrieve the file from Azure.
  • The controller will receive the URL: https://myaccount.files.core.windows.net/path/?=accesskey

Until the user views this passkey, it eventually expires, and so that the user can bookmark the page, I would like the user not to be redirected to the Azure path, but for ASP.NET to output the file as if the user still located at https://website.com/path/subpath/file.pdf

So the final question is basically:

How can I output a file without forced download and without specifying the path / url of the file?

+5
source share
1 answer

You can try to read the file from your storage as a byte array and use the File method to return it from the action method.

 public ActionResult View(int id) { // id is a unique id for the file. Use that to get the file from your storage. byte[] byteArrayOfFile=GetFileInByteArrayFormatFromId(id); return File(byteArrayOfFile,"application/pdf"); } 

Assuming GetFileInByteArrayFormatFromId returns the byte version of the file after reading from your repository / azure. You might want to consider caching certain files in your environment so that you don’t have to turn to azure writing to get it for every request.

If you can read the file as a file stream, the File method has an overload, which also takes this value

 public ActionResult View(int id) { // id is a unique id for the file. Use that to get the file from your storage. FileStream fileStream = GetFileStreamFromId(id);; return File(fileStream, "application/pdf","Myfile.pdf"); } 

And if you have a file available on your server (cached files), you can use another overload of the File method, where you will pass the path instead of an array of bytes.

 public ActionResult View(int id) { var f = Server.MapPath("~/Content/Downloads/sampleFile.pdf"); return File(f,"application/pdf"); } 

If the browser has support for displaying the type of response content, the response will be displayed in the browser. Most major browsers support PDF rendering.

There is another overload of the File method, which uses the name of the download file, which will use the browser save / load dialog so that the user can save his local computer and / or open it.

 public ActionResult View(int id) { var pathToTheFile=Server.MapPath("~/Content/Downloads/sampleFile.pdf"); return File(pathToTheFile, MimeMapping.GetMimeMapping(pathToTheFile),"Myfile.pdf"); } public ActionResult ViewFromByteArray(int id) { byte[] byteArrayOfFile=GetFileInByteArrayFormatFromId(id); return File(byteArrayOfFile, "application/pdf","Myfile.pdf"); } 

In this case, the user will receive an invitation to download from the browser.

+7
source

Source: https://habr.com/ru/post/1239153/


All Articles