Click here to Skip to main content
14,732,999 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I have some simple file download code to serve up a file for the user to download - it works fine.
But if I try to server up 2 files by calling the code teice it blows up with the error "Server cannot append header after http headers have been sent" occurabove right where it would create the "open, Save" dialog
I tried to look for some response properties that would tell me the first download was not finished yet but did not see any.
How can I serve up multiple files for download?
Here is my code:
public bool DownloadFile(string targetFileName, string userName, string passWord, Page page1, string t_f)
        {
            try
            {
            string originalfile = page1.Server.MapPath(targetFileName);
            //page1.Response.Write(originalfile);
            //return true;
               FileInfo file=new FileInfo(originalfile);
               long fileSize = file.Length;
                string URI = targetFileName;
               
                    System.IO.Stream responseStream =(Stream) file.OpenRead();
                    int read = 1;
                    page1.Response.Clear();
                    page1.Response.ContentType = "application/octet-stream";
                    page1.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + t_f + "\"");
                    if (fileSize != 0)
                        page1.Response.AddHeader("Content-Length",fileSize.ToString());
             
                    while (read > 0)
                    {
                        byte[] buffer = new byte[1024];
                        read = responseStream.Read(buffer, 0, buffer.Length);
                        page1.Response.OutputStream.Write(buffer, 0, read);
                        page1.Response.Flush();
                    }
                    //page1.Response.Close();
                    //page1.Response.End();
                    responseStream.Close();
                return true;
            }
            catch (Exception e1) { throw new Exception("Download Fails", e1); return false; }
        }

Thanks
Posted

Well i dont think that is possible. The workaround would be zip all files in a single zip file and then download it.
checkout this link:
http://www.4guysfromrolla.com/articles/092910-1.aspx[^]

The problem here is client can make only 1 request at a time and logically for 1 request there should be 1 response. The header of the request can be sent only once and before every other body data of the response and once it is sent the client browser starts taking action acording to the header received for that request.

when you Flush() the response it sends your header and the data added to the output stream.

Plus i dont understand how will a client can request multiple files in a single click? there must be a link on his screen reads like "Download All" or something similar. And if anyhow you are able to allow multiple file download how will a client manage when he will continously get 20-25 files to download/save/open on after one?

so the best answer is comppress all files in a zip and send the compressed zip to clinet.

Thanks,
Hemant
   
You can only have one header: you already have one on your page.
Try this first:
page1.Response.Clear();           // Already have this
page1.Response.ClearContent();    // Add this line
page1.Response.ClearHeaders();    // Add this line
   
Comments
JJamesCChowdare 2-May-17 8:05am
   
and need check this adding header is adding some where in application,if then need override or comment according,for me resolved this by commenting.
OriginalGriff 2-May-17 8:27am
   
Two things:
1) That was written 6 years ago!
2) Your comment makes no sense at all in English - I cannot understand at all what you are trying to say...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900