Provide static file download at server in web application

前端 未结 1 873
小鲜肉
小鲜肉 2021-01-25 22:54

I am working on an MVC 4 web application. On one page I am providing an anchor link which refers to a file on application\'s directory. The code of the same is -



        
相关标签:
1条回答
  • 2021-01-25 23:16

    Your anchor seem to be pointing to a controller action named Download_Static_File (which unfortunately you haven't shown) and passing a parameter called File_Path.

    The @Html.Action helper that you are using in your view is attempting to execute the specified action as a child action. You may find the following blog post useful which describes child actions: http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx/

    I guess that what you are trying to achieve is to generate an anchor in your view pointing to the static file which can be downloaded by the user. In this case you'd rather use an anchor tag in conjunction with the Url.Action helper:

    <a href="@Url.Content("~/Content/Templates/Pt_Data/Pt_Data.xls")">
        Download_Static_File
    </a>
    

    This assumes that your web application has a folder called Content/Templates/Pt_Data under the root containing a file named Pt_Data.xls which will be downloaded by the user when he clicks upon this anchor tag.

    If on the other hand the file that you want to be downloaded by the user is situated in a folder which is not publicly accessible from the client (for example the ~/App_Data folder) you might in this case have a controller action on your server that will stream the file:

    public ActionResult DownloadStaticFile(string filename)
    {
    
        string path = Server.MapPath("~/App_Data");
        string file = Path.Combine(path, filename);
        file = Path.GetFullPath(file);
        if (!file.StartsWith(path))
        {
            throw new HttpException(403, "Forbidden");
        }
        return File(file, "application/pdf");
    }
    

    and then in your view you would have an anchor to this controller action:

    @Html.ActionLink(
        linkText: "Download template", 
        actionName: "DownloadStaticFile", 
        controllerName: "Charge_Entry", 
        routeValues: new { filename = "Pt_Data.xls" },
        htmlAttributes: null
    )
    
    0 讨论(0)
提交回复
热议问题