0

I have to redirect to the same page after a file download !

when i say response.redirect .. it says Cannot redirect after HTTP headers have been sent

can some one help me with this?

i am zipping a file using ionic.zip and downloading it.

PS : Let me know if i have to make my question more clear i can explain :(

    Response.Clear();
    Response.BufferOutput = false;
    string filename = "results" + ".zip";
    Response.ContentType = "application/zip";
    Response.AddHeader("content-disposition", "filename=" + filename);

    using (ZipFile zip = new ZipFile())
    {
        zip.AddDirectory(pathhdf.Value);
        zip.Save(Response.OutputStream);
    }

    Response.Close();

   Response.Redirect("Default.aspx"); /// here come my error

Thanks in advance!

helpme
  • 570
  • 1
  • 7
  • 26
  • 1
    Seems to be a similar question like this: http://stackoverflow.com/questions/10778426/how-not-to-abort-http-response-c-sharp – Quintium Jun 12 '12 at 19:10

3 Answers3

3

You can't send a redirect header, or any other HTTP headers, after you send HTTP content. In this case your HTTP content is your .zip file, so you can't send a redirect header after you write the .zip file to the output stream.

Dan O
  • 6,022
  • 2
  • 32
  • 50
2

You cannot redirect from the server because you don't know how long it will take for the client to download the file. You could use javascript interval to poll for the presence of a cookie that the server could emit. Here's a nice article explaining this. Once the cookie is detected the client knows that download has finished and you could redirect using for example window.location.href. And here's a similar post.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

You can't make a redirect after sending a file. There can only be one response for one request, and the redirect is a response in itself.

If you want to make both the download and the redirect, you have to send two requests from the client. Start the download, then use setTimeout to make the redirect to Default.aspx after a delay long enough to know that the donload has started. The server will only reply to one request at a time, so the Default.aspx page will load after the download is complete.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005