adding header to http response in an action inside a controller in asp.net/mvc

前端 未结 5 2098
花落未央
花落未央 2021-02-13 18:14

I am streaming data from server to client for download using filestream.write. In that case what is happening is that I am able to download the file but it does not

5条回答
  •  灰色年华
    2021-02-13 18:48

    Please take a look here.

    Following is taken from referenced website.

    public FileStreamResult StreamFileFromDisk()
    {
        string path = AppDomain.CurrentDomain.BaseDirectory + "uploads/";
        string fileName = "test.txt";
        return File(new FileStream(path + fileName, FileMode.Open), "text/plain", fileName);
    }
    

    Edit 1:

    Adding something that might be more of your interest from our good ol' SO. You can check for complete detail here.

    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);
    }
    

提交回复
热议问题