I cannot download a file by actionlink mvc5

I know the error is in the way I'm interpreting the routine. I have a routine to download an attached file in a table. This file was generated by my application (binary) and I now need to write this binary to any dir, i.e. in My table/grid there are several files and I need to write the ID file such. Well, I've tried several ways and I know one detail I'm not getting. See what I've done: CONTROLLER:

public FileResult Download(int id)
        {
            int _arquivoId = id;
            var arquivos = oModelFiles.GetFileReport(id);

            string nomeArquivo = (from arquivo in arquivos
                                  where arquivo.ID_SOLIC_RELATORIO == _arquivoId
                                  select arquivo.BL_RELATORIO).First().ToString();//iSSO AQUI É TENTATIVA.

            string contentType = "application/pdf";
            return File(nomeArquivo, contentType, "report.pdf");
        }

My class for grab file

public class ModelFiles
    {
        public List<POC_SOLIC_RELATORIO> GetFileReport(int _Id_Solic_Relat)
        {
            List<POC_SOLIC_RELATORIO> lstFiles = new List<POC_SOLIC_RELATORIO>();
            //DirectoryInfo dirInfo = new DirectoryInfo(HostingEnvironment.MapPath("~/Arquivos"));
            DirectoryInfo dirInfo = new DirectoryInfo("C:/Relatemp/");
            string arquivoCaminho = string.Empty;
            int i = 0;
            foreach (var item in dirInfo.GetFiles())
            {
                lstFiles.Add(new POC_SOLIC_RELATORIO()
                {
                    ID_RELATORIO = _Id_Solic_Relat,
                    //arquivoID = i + 1,
                    //arquivoNome = item.Name,
                    //FilePath = dirInfo.FullName + @"\" + item.Name
                });
                i = i + 1;
            }
            return lstFiles;
        }
    }

My view where has the Download button

<table class="table">
    <tr>
        <th>
            @*@Html.DisplayNameFor(model => model.POC_RELATORIO.NM_RELATORIO)*@
            @Html.DisplayName("Nome do Relatório")
        </th>
        <th>
            @Html.DisplayName("Relatório")
        </th>
        <th>
            @*@Html.DisplayNameFor(model => model.ID_USUARIO)*@
            @Html.DisplayName("Usuário")
        </th>
        <th>
            @*@Html.DisplayNameFor(model => model.DT_SOLICITACAO)*@
            @Html.DisplayName("Data da Solicitação")
        </th>
        <th>
            @*@Html.DisplayNameFor(model => model.DT_AGENDAMENTO)*@
            @Html.DisplayName("Data do Agendamento")
        </th>
        <th>
            @*@Html.DisplayNameFor(model => model.DT_GERACAO)*@
            @Html.DisplayName("Data da Geração do Relatório")
        </th>
        <th>
            @*@Html.DisplayNameFor(model => model.BL_RELATORIO)*@
            @Html.DisplayName("Relatório")
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.POC_RELATORIO.NM_RELATORIO)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ID_SOLIC_RELATORIO)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ID_USUARIO)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DT_SOLICITACAO)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DT_AGENDAMENTO)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DT_GERACAO)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.BL_RELATORIO)
        </td>
        <td>
            @Html.ActionLink("Download", "Download", new { item.ID_SOLIC_RELATORIO, item.BL_RELATORIO })
        </td>
        <td>
            @Html.ActionLink("Open", "", "")
        </td>
        @*<td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID_SOLIC_RELATORIO }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID_SOLIC_RELATORIO }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID_SOLIC_RELATORIO })
        </td>*@
    </tr>
}

</table>

The way it is, this is the error you are giving:

Server Error in application'/'.

The parameters dictionary contains a null entry for parameter ' id ' of non-nullable type ' System. Int32 'for method' System.Web.Mvc.FileResult Download (Int32) ' in ' report.Controllers.AppealReportController'. Year optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

Description: an untreated exception occurred while running the current Web request. Examine the stack trace to get more information about the error and where it originated in the code.

Exception details: System.ArgumentException: the parameters dictionary contains a null entry for parameter ' id ' of non-nullable type ' System. Int32 'for method' System.Web.Mvc.FileResult Download (Int32) ' in ' report.Controllers.AppealReportController'. Year optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

Source error:

Untreated exception was generated during the execution of the current web request. Information relating to the origin and location of the exception can be identified using Stack tracking exception below.

Stack tracking:

Author: pnet, 2015-11-09

1 answers

You are on the right track. Let's just modify a few things:

Here:

    public FileResult Download(int id)
    {
        int _arquivoId = id;
        var arquivos = oModelFiles.GetFileReport(id);

        string nomeArquivo = (from arquivo in arquivos
                              where arquivo.ID_SOLIC_RELATORIO == _arquivoId
                              select arquivo.BL_RELATORIO).First().ToString();//iSSO AQUI É TENTATIVA.

        string contentType = "application/pdf";
        return File(nomeArquivo, contentType, "report.pdf");
    }

The error says that you are not passing id in the address. I don't know what you called it, but I believe it was http://endereco/Arquivos/Download. It should be http://endereco/Arquivos/Download/5, where 5 is the id.

The second thing is here:

return File(nomeArquivo, contentType, "report.pdf");

nomeArquivo need to be assembled before with Server.MapPath, otherwise it doesn't work. For example:

nomeArquivo = Server.MapPath(nomeArquivo);

EDIT

By comment, You me said the URL used is this:

http://localhost:55839/AppealReport/Download?ID_SOLIC_RELATORIO=1&BL_RELATORIO=System.Byte%5B%5D

But notice that the signature of action does not have these parameters:

public FileResult Download(int id)

If you need more parameters in the action , you need to declare them. For example:

public FileResult Download(int SolicitanteId, bool ImprimirEmTela) { ... }

URL parameters need to have exactly the same names passed as parameter:

http://localhost:55839/AppealReport/Download?SolicitanteId=1&ImprimirEmTela=true
 2
Author: Leonel Sanches da Silva, 2015-11-09 17:27:16