Returning a file to View/Download in ASP.NET MVC

前端 未结 9 1608
刺人心
刺人心 2020-11-21 22:41

I\'m encountering a problem sending files stored in a database back to the user in ASP.NET MVC. What I want is a view listing two links, one to view the file and let the mim

9条回答
  •  你的背包
    2020-11-21 22:44

    public ActionResult Download()
    {
        var document = ...
        var cd = new System.Net.Mime.ContentDisposition
        {
            // for example foo.bak
            FileName = document.FileName, 
    
            // always prompt the user for downloading, set to true if you want 
            // the browser to try to show the file inline
            Inline = false, 
        };
        Response.AppendHeader("Content-Disposition", cd.ToString());
        return File(document.Data, document.ContentType);
    }
    

    NOTE: This example code above fails to properly account for international characters in the filename. See RFC6266 for the relevant standardization. I believe recent versions of ASP.Net MVC's File() method and the ContentDispositionHeaderValue class properly accounts for this. - Oskar 2016-02-25

提交回复
热议问题