简体   繁体   English

剃刀链接以从Controller读取文件路径

[英]Razor link to read file path from Controller

I'm quite new to the concept of razor. 我对剃刀的概念很陌生。 Basically, I need to download .pdf files. 基本上,我需要下载.pdf文件。 The paths are stored in my database. 路径存储在我的数据库中。

I need to have a hyperlink on my cshtml which will call a function inside my Controller. 我需要在cshtml上建立一个超链接,该超链接将在Controller中调用一个函数。 Then, on my controller, it will fetch the path of my file, which will return to my cshtml and do a windows.open(path). 然后,在我的控制器上,它将获取文件的路径,该文件的路径将返回到我的cshtml并执行windows.open(path)。

Please let me know how can this be possible or perhaps a better approach to perform what I need to do. 请让我知道这怎么可能,或者是执行我需要做的更好的方法。

Thanks. 谢谢。

Example: 例:

.cshtml .cshtml

@Html.ActionLink (
linkText: "My Link",
actionName: "DownloadFiles",
controllerName: "Files",
routeValues: new 
{
   fileId: id,
   fileType: "PDF"
},
htmlAttributes: null

windows.open(path);

Controller 控制者

public ActionResult DownloadFiles(int fileId, string fileType) 
{
   string FilePath = "";
   FilePath = service.GetFilePath(fileId,fileType);

   return FilePath;
}

You have to return fileresult which is a child class for ActionResult: 您必须返回fileresult,它是ActionResult的子类:

public FileResult DownloadFile()
{
   string FilePath = "";
   FilePath = service.GetFilePath(fileId,fileType);

   // add virtual file path only
   return File(FilePath, "application/pdf");
}

Here virtual path can be "~/Uploadfile/mypdf.pdf" 这里的虚拟路径可以是“〜/ Uploadfile / mypdf.pdf”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM