Returning a downloadable file using a stream in asp.net web forms

后端 未结 3 786
旧巷少年郎
旧巷少年郎 2020-12-10 05:20

In asp.net MVC I can do something like the following which will open a stream:

 Stream strm1 = GenerateReport(Id);

return File(strm1, 
            \"applica         


        
3条回答
  •  孤街浪徒
    2020-12-10 05:47

    I use this extension to send a stream as a downloadable file:

    public static class ToDownloadExtention
    {
       public static void ToDownload(this Stream stream, string fileName, HttpResponse response)
        {
            response.Clear();
            response.ContentType = "application/octet-stream";
            response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", fileName));
            stream.CopyTo(response.OutputStream);
            response.End();
        }
    }
    

    And the usage is:

    var stream = new MemoryStream();
    
    stream.ToDownload("someFileName.ext",Response);
    

提交回复
热议问题