如何服务于MVC文件而不暴露的网址是什么? [英] How to serve a file in MVC without exposing the URL?

查看:202
本文介绍了如何服务于MVC文件而不暴露的网址是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个树状浏览器它允许用户浏览文件和子目录,当用户到达某个文件,网站将转到 https://website.com/path/子路径/ file.pdf 。假设我可以识别用户正在浏览的文件时,会发生以下情况:


  • 控制器将产生一个SAS键检索Azure的文件。

  • 控制器将获得一个网址: https://myaccount.files.core.windows.net/path/?=accesskey

虽然是与用户查看此访问密钥没有问题,它最终将到期,并为用户书签的页面,我想用户的不可以被重定向到Azure的路径的,但为ASP.NET到输出文件,如果用户仍处于 https://website.com/path/subpath/file.pdf

那么到底问题基本上是:


  

我怎么能输出的文件,不强制下载并没有显示文件路径/网址是什么?



解决方案

您可以尝试读取存储的文件作为一个字节数组,并使用文件方法从操作方法返回。

 公众的ActionResult视图(INT ID)
{
  // id是该文件的唯一ID。用它来获得您的存储文件。
  字节[] = byteArrayOfFile GetFileInByteArrayFormatFromId(ID);
  返回文件(byteArrayOfFile,应用程序/ PDF格式);
}

假设 GetFileInByteArrayFormatFromId 从存储/蔚蓝看完后返回该文件的字节数组版本。您可以考虑一些缓存文件在你的环境,让您无需接触到蔚蓝得到它在每次请求。

如果你有可用的文件服务器(缓存文件),可以使用文件方法的另一个重载,你会传递路径,而不是字节数组。

 公众的ActionResult视图(INT ID)
{
  变种F =使用Server.Mappath(〜/内容/下载/ sampleFile.pdf);
  返回文件(F,应用程序/ PDF格式);
}

根据浏览器的设置,上述2的方法要么询问用户是否希望下载或打开文件或只需下载/打开该文件。如果preFER直接在浏览器中显示文件的内容,你可以发送一个FILESTREAM到浏览器。

 公众的ActionResult视图(INT ID)
{
    VAR pathToTheFile =使用Server.Mappath(〜/内容/下载/ sampleFile.pdf);
    VAR FILESTREAM =新的FileStream(pathToTheFile,
                                        FileMode.Open,
                                        FileAccess.Read
                                    );
    返回新FileStreamResult(FILESTREAM,应用程序/ PDF格式);
}

I have a tree-viewer which allows for the user to browse files and sub-directories, when the user reaches a file, the website will go to https://website.com/path/subpath/file.pdf. Assuming I can identify that the user is viewing a file, the following will happen:

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

While there is no problem with the user viewing this access key, it will eventually expire, and for the user to 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 is still at https://website.com/path/subpath/file.pdf

So the end question is basically:

How can I output a file, without forcing download and without showing the file path/url?

解决方案

You may 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 array version of the file after reading from your storage/azure. You may consider caching some of the files in your environment so that you do not need to reach out to azure to get it on every request.

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

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

Based on the browser setting, the above 2 approaches will either ask user whether he wishes to download or open the file or simply download/open the file. If you prefer to show the file content directly in the browser, you may send a filestream to the browser.

public ActionResult View(int id)
{
    var pathToTheFile=Server.MapPath("~/Content/Downloads/sampleFile.pdf");
    var fileStream = new FileStream(pathToTheFile,
                                        FileMode.Open,
                                        FileAccess.Read
                                    );
    return  new FileStreamResult(fileStream, "application/pdf");           
}

这篇关于如何服务于MVC文件而不暴露的网址是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆