Server cannot clear headers after HTTP headers have been sent.

Starr Duskk
Starr Duskk used Ask the Experts™
on
I am sending a file with  HttpContext.Current.Response

I need to loop through, create a file, send it to the browser for download. Then create another file and send it to the browser for download.

On the second one I get an error:
Server Cannot Append Header After HTTP headers have been sent Exception

So I added this:
HttpContext.Current.Response.ClearHeaders()

and now get this error:
Server cannot clear headers after HTTP headers have been sent.

Any solutions for sending multiple files without intervention from the user? (someone said to put a button for each file, that is not an option, as we are sending a file for every 100 pages as an example and don't know how many files we might have and we could be spamming a ton of buttons to the screen and making them click them all and remember which one they clicked).

Someone else suggested zipping up the files, but due to most of our clients not being allowed to install on their machines and people all fretting over viruses in zip files, the boss does not see that as a solution.

What solutions do you recommend that you have used?

Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Prepress/OMS Specialist
Commented:
What you are trying to do is intentionally not allowed due to security reasons.  I understand the concern about ZIP files, but if your users are expecting a ZIP file as a download, I don't see what the issue could be.  Also, every modern OS has the ability to open ZIP files, so no need to install any additional software.

Here is a link to an article describing a technique to do this: Downloading multiple files in ASP.NET
Commented:
Zip files are the way to go. I work with similar clients that have locked-down permissions but like Jim said, every modern or semi-modern OS has zip functionality built into the shell and requires no software to open. Viruses are a non-issue if the zip files are coming directly from you (unless you're intentionally injecting viruses). Files cannot magically become infected without malicious intervention of some sort.

There's actually the same (if not higher) amount of risk of a virus from sending individual files, so it makes no sense whatsoever to not use zip files. On top of that, zip file compression could potentially save you and your user a lot of bandwidth.

Use a zip file.

As for the error you're seeing - that's because every standard HTTP request has one HTTP response (which has one set of response headers and one response body). The headers have to be sent before the body, so once the response body begins, the headers are considered to be finished and sent. So you can't continue to send another set of headers and another body in the same response.

You could use JavaScript to fire off multiple requests but if you have 10 files, that's going to prompt the user 10 times. It's a bad user experience to do it like that.
Starr DuskkASP.NET VB.NET Developer

Author

Commented:
Boss decided to limit them to 500 records maximum since he thinks they'd be confused by a ZIP file.

Okaaaay. LOL

I'm sure this will be coming back down the road.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial