"Server cannot append header after HTTP headers have been sent " in multiple file downloadhttp://forums.asp.net/t/1217644.aspx/1?+Server+cannot+append+header+after+HTTP+headers+have+been+sent+in+multiple+file+downloadTue, 17 Jun 2008 06:44:08 -040012176442163594http://forums.asp.net/p/1217644/2163594.aspx/1?+Server+cannot+append+header+after+HTTP+headers+have+been+sent+in+multiple+file+download"Server cannot append header after HTTP headers have been sent " in multiple file download <p>Hope this is the right forum!</p> <p>I have&nbsp;some simple file download code to serve up a file for the user to download - it works fine.</p> <p>But if I try to server up 2 files by calling the code teice it blows up with the error above right where it would create the &quot;open, Save&quot; dialog</p> <p>&nbsp;I tried to look for some response properties that would tell me the first download was not finished yet but did not see any.</p> <p>How can I serve up multiple files for download?</p> <p>Here is my code:</p> <p>&nbsp;Response.ContentType = &quot;application/X-unknown&quot;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.AddHeader(&quot;Content-Disposition&quot;, &quot;attachment; filename=&quot;&quot;&quot; &amp; myFileName &amp; &quot;&quot;&quot;&quot;)</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.TransmitFile(Folderpath &amp; myFileName)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.Flush()<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.Close()</p> <p>&nbsp;</p> <p>&nbsp;Thanks</p> <p>&nbsp;Bill</p> 2008-02-09T18:43:59-05:002163614http://forums.asp.net/p/1217644/2163614.aspx/1?Re+Server+cannot+append+header+after+HTTP+headers+have+been+sent+in+multiple+file+downloadRe: "Server cannot append header after HTTP headers have been sent " in multiple file download <p>Due to the limitations of the http protocol, you can only send one file per request.&nbsp; Therefore, you will need to pack the separate files into one package (zip, for example) and send that.&nbsp; Something like this should help get you started: <a href="http://weblogs.asp.net/dneimke/archive/2005/02/25/380273.aspx">http://weblogs.asp.net/dneimke/archive/2005/02/25/380273.aspx</a></p> <p>&nbsp;</p> 2008-02-09T19:14:49-05:002163683http://forums.asp.net/p/1217644/2163683.aspx/1?Re+Server+cannot+append+header+after+HTTP+headers+have+been+sent+in+multiple+file+downloadRe: "Server cannot append header after HTTP headers have been sent " in multiple file download <p>Are you sure?</p> <p>If I could tell that the first download had not finished then I could wait until serving up the next file.</p> <p>somehting like&nbsp;</p> <p>while &nbsp;response.someproperty = true then </p> <p>sleep (1000)</p> <p>loop</p> <p>or, if there was a way to specify multiple attachments then I could do them all at once.</p> <p>something like &nbsp;Response.AddHeader(anotherfile)</p> <p>&nbsp;</p> <p>I really don't want to send a zip unless I have to&nbsp;</p> <p>&nbsp;</p> 2008-02-09T21:04:18-05:002163737http://forums.asp.net/p/1217644/2163737.aspx/1?Re+Server+cannot+append+header+after+HTTP+headers+have+been+sent+in+multiple+file+downloadRe: "Server cannot append header after HTTP headers have been sent " in multiple file download <p>You could try a Response.Redirect back to the same page after each download (after Response.Flush), perhaps passing in a querystring value to determine which file should be next.&nbsp; I've not tried this, but it might work.</p> <p>&nbsp;</p> 2008-02-09T22:22:02-05:002427353http://forums.asp.net/p/1217644/2427353.aspx/1?Server+cannot+append+header+after+HTTP+headers+have+been+sentServer cannot append header after HTTP headers have been sent <p><font size="2">I am getting an error, Server cannot append header after HTTP headers have been sent.<br> I am trying to download a file from ftp server. I have a grid which displays the list and when the user selects one, I open a pop-up where the download occurs in page_load().It is working fine but when I try to show an image while the download process is going on , I get the above mentioned error.<br> /*****************Code for image display**************************/<br> const string MAIN_IMAGE = &quot;/_layouts/images/novartis/waiting.gif&quot;;<br> const int PROGRESS_BAR_SIZE = 10; //number of steps in your progress bar<br> const string PROGRESS_BAR_STEP = &quot;/_layouts/images/novartis/pik.gif&quot;; //image for idle steps<br> const string PROGRESS_BAR_ACTIVE_STEP = &quot;/_layouts/images/novartis/pik.gif&quot;; //image for active step <br> Response.Write(&quot;&lt;div id=\&quot;mydiv\&quot; align=\&quot;center\&quot;&gt;&quot;);<br> Response.Write(&quot;&lt;img src=\&quot;&quot; &#43; MAIN_IMAGE &#43; &quot;\&quot;&gt;&quot;);<br> Response.Write(&quot;&lt;div id=\&quot;mydiv2\&quot; align=\&quot;center\&quot;&gt;&quot;);<br> for (int i = 1; i &lt;= PROGRESS_BAR_SIZE; i&#43;&#43;)<br> {<br> Response.Write(&quot;&lt;img id='pro&quot; &#43; i.ToString() &#43; &quot;' src='&quot; &#43; PROGRESS_BAR_STEP &#43; &quot;'&gt;&amp;nbsp;&quot;);<br> }<br> Response.Write(&quot;&lt;/div&gt;&quot;);<br> Response.Write(&quot;&lt;/div&gt;&quot;);<br> Response.Write(&quot;&lt;script language=javascript&gt;&quot;);<br> Response.Write(&quot;var counter=1;var countermax = &quot; &#43; PROGRESS_BAR_SIZE &#43; &quot;;function ShowWait()&quot;);<br> Response.Write(&quot;{ document.getElementById('pro' &#43; counter).setAttribute(\&quot;src\&quot;,\&quot;&quot; &#43; PROGRESS_BAR_ACTIVE_STEP &#43; &quot;\&quot;); if (counter == 1) document.getElementById('pro' &#43; countermax).setAttribute(\&quot;src\&quot;,\&quot;&quot; &#43; PROGRESS_BAR_STEP &#43; &quot;\&quot;);else {var x=counter - 1; document.getElementById('pro' &#43; x).setAttribute(\&quot;src\&quot;,\&quot;&quot; &#43; PROGRESS_BAR_STEP &#43; &quot;\&quot;);} counter&#43;&#43;;if (counter &gt; countermax) counter=1;}&quot;);<br> Response.Write(&quot;function Start_Wait(){mydiv.style.visibility = \&quot;visible\&quot;;window.setInterval(\&quot;ShowWait()\&quot;,1000);}&quot;);<br> Response.Write(&quot;function Stop_Wait(){ mydiv.style.visibility = \&quot;hidden\&quot;;window.clearInterval();}&quot;);<br> Response.Write(&quot;Start_Wait();&lt;/script&gt;&quot;);<br> Response.Flush();<br> <br> /**************Code for download**********************/<br> <br> if (Request.QueryString[&quot;DownloadUrl&quot;] != null)<br> {<br> FtpWebRequest reqFTP;<br> //HtmlControl frame1 = (HtmlControl)this.FindControl(&quot;frame1&quot;);<br> //frame1.Attributes[&quot;src&quot;] = &quot;Iframe1WebForm.aspx&quot;;<br> try<br> {<br> string fileName = Request.QueryString[&quot;DownloadUrl&quot;].ToString();<br> string filename = Path.GetFileName(fileName);<br> WriteLog(filename);<br> Response.Buffer = true;<br> Response.Clear();<br> Response.AddHeader(&quot;Content-Disposition&quot;, &quot;attachment; filename=&quot; &#43; filename);<br> Response.ContentType = &quot;application/octet-stream&quot;;<br> ////Response.AddHeader(&quot;Content-Disposition&quot;, &quot;attachment; filename=&quot; &#43; filename);<br> Response.Flush();<br> reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(&quot;ftp://&quot; &#43; ftpServerIP &#43; &quot;/&quot; &#43; filename));<br> WriteLog(req</font><a href="http://www.west-wind.com/weblog/FTP.RequestUri.ToString"><font size="2">FTP.RequestUri.ToString</font></a><font size="2">());<br> req</font><a href="http://www.west-wind.com/weblog/FTP.Proxy"><font size="2">FTP.Proxy</font></a><font size="2"> = null;<br> req</font><a href="http://www.west-wind.com/weblog/FTP.Method"><font size="2">FTP.Method</font></a><font size="2"> = WebRequestMethods.</font><a href="http://www.west-wind.com/weblog/Ftp.DownloadFile;"><font size="2">Ftp.DownloadFile;</font></a><br> <font size="2">req</font><a href="http://www.west-wind.com/weblog/FTP.UseBinary"><font size="2">FTP.UseBinary</font></a><font size="2"> = true;<br> req</font><a href="http://www.west-wind.com/weblog/FTP.Credentials"><font size="2">FTP.Credentials</font></a><font size="2"> = new NetworkCredential(ftpUserID, ftpPassword, ftpDomain);<br> //WebResponse aa = req</font><a href="http://www.west-wind.com/weblog/FTP.GetResponse"><font size="2">FTP.GetResponse</font></a><font size="2">();<br> FtpWebResponse response = (FtpWebResponse)req</font><a href="http://www.west-wind.com/weblog/FTP.GetResponse"><font size="2">FTP.GetResponse</font></a><font size="2">();<br> Stream ftpStream = response.GetResponseStream();<br> MemoryStream MemStream = new MemoryStream();<br> int bufferSize = 102400;//2048;<br> byte[] respBuffer = new byte[bufferSize];<br> try<br> {<br> int bytesRead = ftpStream.Read(respBuffer, 0, respBuffer.Length);<br> WriteLog(&quot;byteRead&quot; &#43; &quot; &quot; &#43; bytesRead.ToString());<br> while (bytesRead &gt; 0)<br> {<br> MemStream.Write(respBuffer, 0, bytesRead);<br> bytesRead = ftpStream.Read(respBuffer, 0, respBuffer.Length);<br> WriteLog(&quot;inside while&quot; &#43; bytesRead.ToString());<br> }<br> byte[] finalByte = MemStream.GetBuffer();<br> Response.BinaryWrite(finalByte);<br> }<br> finally<br> {<br> ftpStream.Close();<br> response.Close();<br> }<br> }<br> catch (Exception ex)<br> {<br> throw ex;<br> } </font><br> </p> 2008-06-17T06:44:08-04:00