Home Razor link to read file path from Controller
Reply: 1

Razor link to read file path from Controller

carl
1#
carl Published in 2017-12-04 12:03:34Z

I'm quite new to the concept of razor. Basically, I need to download .pdf files. The paths are stored in my database.

I need to have a hyperlink on my cshtml which will call a function inside my 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).

Please let me know how can this be possible or perhaps a better approach to perform what I need to do.

Thanks.

Example:

.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;
}
Manprit Singh Sahota
2#
Manprit Singh Sahota Reply to 2017-12-04 12:15:28Z

You have to return fileresult which is a child class for 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"

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.36683 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO