I can’t download a file by Actionlink mvc5

Asked

Viewed 338 times

0

I know the error is in the way I am 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 such. Well, I tried several ways and I know one 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 the Download button is

<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, that’s the mistake you’re making:

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'. An 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 during the execution of current web request. Examine stack tracking 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'. An optional Parameter must be a Reference type, a nullable type, or be declared as an optional Parameter. Parameter name: Parameters

Error of Origin:

Untreated exception was generated during the current execution web request. The information related to the origin and location of the exception can be identified by using stack tracking exception below.

Cell Trace:

  • What’s wrong in the post to receive the downvote?

1 answer

2


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 mistake says you’re not passing id address. I don’t know what you called it, but I believe it was http://endereco/Arquivos/Download. Should be http://endereco/Arquivos/Download/5, where 5 is the id.

The second thing is here:

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

nomeArquivo needs to be mounted before with Server.MapPath, otherwise it doesn’t 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 signature of Action does not have these parameters:

public FileResult Download(int id)

If you need more parameters in Action, 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
  • When I click the download button, my url looks like this: http://localhost:55839/Appealreport/Download? ID_SOLIC_RELATORIO=1&BL_RELATORIO=System.Byte%5B%5D, so I’m passing the parameter or not?

  • It is not. You are passing two parameters that the Action even recognizes: ID_SOLIC_RELATORIO and BL_RELATORIO. In addition, they are out of the standard. I will adjust my answer to see if it becomes clearer.

  • See now if you’re better to understand.

  • 1

    That was the problem, and I didn’t know I should have the same name. For me, if I were to have an argument with the same type, clear in the correct position, it would fill the argument with the corresponding parameter and it would not depend on the name, but one more that I learned...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.