0

How to download a file created in the server's memory to the client without saving it (ASP.NET)?

I've generated an Excel file on server using NetOffice. BUT now, I am not able to download it to the client. I don't want to save a copy of this file on the server.

Can we send the file object created in the memory using Streams?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Temp O'rary
  • 5,366
  • 13
  • 49
  • 109

1 Answers1

1

Indeed you can. Just pass to Response.OutputStream of your HTTP request. Something like this should work (where yourStream is a stream from your in memory excel doc):

    Response.Clear();
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    Response.AddHeader("Content-Disposition", "attachment; filename=myfile.xlsx");
    yourStream.WriteTo(Response.OutputStream);
    Response.Flush();
geedubb
  • 4,048
  • 4
  • 27
  • 38
  • Thanks @geedubb, seems to be a solution to my problem. :) BUT I have a small doubt regarding "yourStream" Object. Is it an object of the MemoryStream Class? – Temp O'rary Nov 29 '13 at 08:52
  • Can we use this object as following: MemoryStream m = new MemoryStream(); m.WriteTo(Response.OutputStream); I might be wrong here, because we might have to pass the bytes of the file to initialize the MemoryStream object. It is because using NetOffice to create Excel file, I only get the objects of ExcelApplication, WorkBook, and WorkSheet classes. Just for your information, when I do WorkBookObject.Visible = true; the excel file become visible at the server. But I dont find a way to get the bytes of workbook object so that I can pass it to the MemoryStream & init it. – Temp O'rary Nov 29 '13 at 09:11
  • Yes, what you suggest with the memoryStream should work, however I am not familiar with NetOffice. can you update your question and share a sample of code which creates your Excel file and I will try to help further? – geedubb Nov 29 '13 at 10:00