简体   繁体   English

下载文件后无法重定向

[英]cannot redirect after downloading a file

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 当我说response.redirect ..它说无法重定向发送HTTP标头后

can some one help me with this? 有人可以帮我弄这个吗?

i am zipping a file using ionic.zip and downloading it. 我正在使用ionic.zip压缩文件并下载。

PS : Let me know if i have to make my question more clear i can explain :( PS:请让我知道我是否必须更清楚地说明我的问题:(

    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! 提前致谢!

You can't send a redirect header, or any other HTTP headers, after you send HTTP content. 发送HTTP内容后,您将无法发送重定向标头或任何其他HTTP标头。 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. 在这种情况下,您的HTTP内容就是您的.zip文件,因此将.zip文件写入输出流后,您将无法发送重定向标头。

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. 您可以使用javascript interval来轮询服务器可能发出的cookie。 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 . 一旦检测到cookie,客户端便知道下载已完成,您可以使用例如window.location.href重定向。 And here's a similar post . 这是一个类似的帖子

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. 开始下载,然后使用setTimeout将延迟重定向到Default.aspx,该延迟足够长的时间才能知道下载已开始。 The server will only reply to one request at a time, so the Default.aspx page will load after the download is complete. 服务器一次只会答复一个请求,因此在下载完成后将加载Default.aspx页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM