I can not download a file by ActionLink mvc5

0

I know the error is in the way I'm interpreting the routine. I have a routine to download a file attached to a table. This file was generated by my application (binary) and now I need to write this binary in any dir, that is, in my table / grid there are several files and I need to write the ID file as such. Well, I tried several ways and I know a detail I'm not getting. Here's 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 to get the 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 you have 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>

As it is, this is the error you are giving:

  

Application Server Error '/'.

     

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' Relatorio.Controllers.AppealReportController '. An   optional parameter must be a reference type, nullable type, or be   declared as an optional parameter. Parameter name: parameters

     

Description: An unhandled exception occurred during the execution of the   current Web request. Examine the stack trace for   more information about the error and where it originated in the code.

     

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

     

Source Error:

     

Unhandled exception was generated during the execution of the current   information. The information relating to the origin and location of the   can be identified using the stack trace of   exception below.

     

Battery Tracking:

    
asked by anonymous 09.11.2015 / 16:55

1 answer

2

You are on the right path. Let's just modify some 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 to the address. I do not 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 id.

The second thing is here:

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

nomeArquivo needs to be mounted before with Server.MapPath ", otherwise it will not work. For example:

nomeArquivo = Server.MapPath(nomeArquivo);

EDIT

By comment, you told me that the URL used is this:

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

But note that the Action signature does not have these parameters:

public FileResult Download(int id)

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

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

URL parameters must have exactly the same names passed as parameters:

http://localhost:55839/AppealReport/Download?SolicitanteId=1&ImprimirEmTela=true
    
09.11.2015 / 17:03