0

I have few links to pdf files store in server in my static page. I would be able to click each link that would open a pdf file in browser. I referred to this link. But it is not working as I intended.

Here is my action method:

public ActionResult GetFileFromServer(string filename)
{
   string folderpath = StrGlobal.file_folder.ToString();
   string filepath = Path.Combine(folderpath, filename);
   filepath = Path.GetfullPath(filepath);

   return File(filepath, "application/pdf");
}

My view:

<p>
   @Html.ActionLink(
   linkText: "ABC Document",
   actionName:"GetFileFromServer",
   controllerName:"StaticPage",
   routeValues:new {filename = "ABC.pdf"},
   htmlAttributes:null
   )
</p>

If I replace and hardcore filename in this line:

string filepath = Path.Combine(folderpath, "ABC.pdf");

It will open that specific pdf file. Otherwise I get an error saying

Value cannot be null. Parameter name: path2

Seems like value is not getting passed from view to controller. How do I fix this issue?

Brad Patton
  • 4,007
  • 4
  • 34
  • 44
PhatLee
  • 37
  • 9
  • Have u tried debugging the action method to see if the parameter is getting populated or not? – akg179 Jun 04 '19 at 18:50
  • Yes, I have tried that. And parameter is not getting populated. – PhatLee Jun 04 '19 at 19:00
  • i think you should try passing just "abc" as filename in `ActionLink()`. If the parameter gets populated, then you might have to change a bit of your action logic – akg179 Jun 04 '19 at 19:21
  • What does the generated link look like on the page? Also have you edited any MapRoutes that might effect routing? – Brad Patton Jun 05 '19 at 17:47

1 Answers1

0

<a href="/staticpath/ABC.pdf" download>
  Donloadpdf
</a>

Hi,

We can resolved issue by easy way with using html download functionality.

user2803730
  • 132
  • 4