문제

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

도움이 되었습니까?

해결책 2

You might want to try to flush the HttpResponse in while loop:

while ((theLine = inputStream.ReadLine()) != null)
{
        streamWriter.WriteLine(theLine);
        streamWriter.Flush(); 
        Response.Flush();
}

다른 팁

Response.BufferOutput = false;

Put that before you start to write data to the Response stream.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top