I have asp.net MVC application that has some anchor elements. The anchor elements lead to static files such as
https://server/website/folder/subFolder/anotherSubFolder/File.pdf
The files are all hosted in the web application domain in IIS. When the link is clicked, this file is opened in a new browser window. Is it possible to mask this URL in the browser? The file should open, but the URL should NOT point to the exact path of the file.
答案 0 :(得分:2)
Sure,
In any Controller
you want, make an Action
by the name File
. The body of the action is:
public ActionResult File(string fileName)
{
var path = System.Web.HttpContext.Current.Server.MapPath("~/files/" + fileName);
var fileStream = new FileStream(path,
FileMode.Open,
FileAccess.Read
);
var fsResult = new FileStreamResult(fileStream, "application/pdf");
return fsResult;
}
and the anchor is:
<a href="@Url.Action("File", "Home", new { filename = "file.pdf" })" target="_blank">Open File</a>
In this way the url becomes something like this:
http://localhost:4268/Home/File?filename=file.pdf