Reputation: 97
I have a .net web application that fetches a large document from a REST service that should be download to the client host.
I want to stream the data so that it seems to download directly on the client. My problem is that is the "File Download" dialog doesn't show up until Response.End() is called. I want it show up instanlty.
// class extends System.Web.UI.Page
HttpClient client = new HttpClient();
// Add an Accept header for the mediatype format.
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(mediaType));
Stream stream = client.GetStreamAsync("http://www.aaa.se/theurl").Result;
StreamReader inputStream = new StreamReader(stream);
Response.ContentType = "text/plain";
Response.AppendHeader("Content-Disposition", "attachment; filename=file.txt");
using (StreamWriter streamWriter = new StreamWriter(Response.OutputStream))
{
streamWriter.AutoFlush = true;
string theLine = null;
while ((theLine = inputStream.ReadLine()) != null)
{
streamWriter.WriteLine(theLine);
streamWriter.Flush(); // <<<---- HERE FileDialog should pop up!
}
}
Response.End(); // <<<--- BUT it pops up here!
Flush and AutoFlush should do the trick here!!?? Can anyone see what I'm doing wrong?
Thanks
Upvotes: 0
Views: 2491
Reputation: 18593
Response.BufferOutput = false;
Put that before you start to write data to the Response stream.
Upvotes: 4
Reputation: 176
You might want to try to flush the HttpResponse in while loop:
while ((theLine = inputStream.ReadLine()) != null)
{
streamWriter.WriteLine(theLine);
streamWriter.Flush();
Response.Flush();
}
Upvotes: 1