How to Generate a Link to download a File in asp.net MVC?

后端 未结 4 2002
梦毁少年i
梦毁少年i 2021-02-04 21:11

I am testing Doddle Report to generate a few report form a IEnumerable object. I need to generate a Link like this

PDF - http://myserver.com/reports/ProductsRepo         


        
4条回答
  •  眼角桃花
    2021-02-04 21:54

    I'd done something similar what you want on MVC 5 and I used FilePathResult as J.W said.

    cshtml:

    @Html.ActionLink("Download Here", "DownloadExampleFiles", "Product", new { fileName = "variation-example" }, new { @target = "_blank" })
    

    ProductController:

    public FilePathResult DownloadExampleFiles(string fileName)
    {
        return new FilePathResult(string.Format(@"~\Files\{0}", fileName + ".txt"), "text/plain");
    }
    

    Download a file seems to be very silly/easy, however, for those who are new on MVC, it's not. This approach sounds to be the best while using MVC.

    PS: I've forced a .txt just as an example. You should do what is intersting for you to get the specific file.

提交回复
热议问题