1

I am trying to create a link so that users can download a file that is already on the server.

In my controller I have:

public ActionResult downloadFile(int id)
        {
            DataAccess da = new DataAccess();
            PersonFile displayFile = new PersonFile();
            displayFile = da.getPersonFileByID(id);

            string mimeType = "application/pdf";

            return File(displayFile.FileData, mimeType, displayFile.FileName);
        }

Then in my view I have:

 <th class="editAddLabel">
        @Html.LabelFor(model => Model.PersonFile.FileName, "FileName")

        *@Html.ActionLink("Download file", "downloadFile", "PersonController", Model.id, null )*

    </th>

It's my first time doing something like this, but I feel like when the person clicks on the link, it should send the id of the model into the downloadFile method and then return the contents of the file. But when I click on the link, I get nothing. And when I set breakpoints on the downloadFile method, nothing happens either leading me to believe that the method is not even called. What could I be doing wrong?

Edit: I've also tried the following:

<input type="button" title="File" value="File" onclick="location.href='@Url.Action("downloadFile", "PersonController",  new { id = Model.id })'; }" />
    </th>

My error:

Server Error in '/' Application.

    The resource cannot be found.

    Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

    Requested URL: /PersonController/downloadFile
FrostyStraw
  • 1,628
  • 3
  • 25
  • 34
  • What do you mean `you get nothing`? What status code and response is the server responding with? – Brad C Jun 24 '15 at 13:36
  • sorry, i added the error message into my question. that is the error when I try to use actionlink. when I use a button, it literally does nothing at all and I get no error messages or antyhing @br4d – FrostyStraw Jun 24 '15 at 13:38
  • 1
    You aren't passing the ID. Make sure your link is properly formed. – Brad C Jun 24 '15 at 13:39

3 Answers3

1

Your ActionLink is malformed and not sending the ID with the request. Try this instead:

@Html.ActionLink("Download file", "downloadFile", "PersonController", new { id = Model.id }, null )
Brad C
  • 2,868
  • 22
  • 33
  • What error message is it giving now? The Requested URL has to have the Id on there now such as `/PersonController/downloadFile/5` so it should hit the action method at least – Brad C Jun 24 '15 at 13:51
  • Server Error in '/' Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. Requested URL: /PersonController/downloadFile/240 – FrostyStraw Jun 24 '15 at 13:54
0

Just a guess, but you could set the return type to "FileResult" like:

public FileResult downloadFile(int id)
    {
        DataAccess da = new DataAccess();
        PersonFile displayFile = new PersonFile();
        displayFile = da.getPersonFileByID(id);

        string mimeType = "application/pdf";

        return File(displayFile.FileData, mimeType, displayFile.FileName);
    }

Further details: Download file of any type in Asp.Net MVC using FileResult?

Community
  • 1
  • 1
Oliver
  • 1,225
  • 10
  • 19
0

Looking at your error, it seems the URL you are requesting is /PersonController/downloadFile. This to me seems wrong, you're not passing an ID that relates to a person, and also that MVC routing should be /person/downloadfile not personcontroller.

Try Url.Action("downloadFile", "Person", new { @id = Model.Id }

ediblecode
  • 11,701
  • 19
  • 68
  • 116