简体   繁体   中英

Response.Redirect does not work

I have a page that exports large amount of data to excel 2007 macro enabled format. I have Response.Redirect at the end to redirect to another page after the excel file is created but the redirect does not work when I have really large amount of data ie about 60,000+ rows. Below is the snippet of the code that I am using to save the excel file:

Response.Clear();
Response.AppendHeader("content-disposition", "attachment; filename=RegionData.xlsm");
Response.ContentType = "application/octet-stream";
wbRegionData.Save(Response.OutputStream);//();
Response.End();
Response.Redirect("RegionData.aspx?PALID=" + AVListID, true);

Thank you for your help.

You cannot do a file download and a redirect in the same server response. If you need to perform a redirect after a successful file download you will need to somehow tell the client that a file download successfully occurred which can only be accomplished by writing a cookie as far as I know.

I have created the jQuery File Download plugin ( Demo ) ( GitHub ) (Blog Posts ) that simplifies a lot of this stuff, using the plugin your code could be like this:

ASP.NET CODE:

Response.Clear();
Response.AppendHeader("content-disposition", "attachment; filename=RegionData.xlsm");
Response.ContentType = "application/octet-stream";
Response.SetCookie(new HttpCookie("fileDownload", "true"){Path = "/"});
wbRegionData.Save(Response.OutputStream);//();
Response.End();

JavaScript:

$.fileDownload("/urltofiledownload/", {
    successCallback: function (url) {
        window.location = "/redirecturl/";
    }
});

Without more of your code I can't give you a complete solution (look at the Demo for examples) but I think using the plugin would be your easiest and best bet.

You can't because you are closing the response by calling Response.End(); You must have to add define these two actions separately (Download and redirection).

It will not work because the Respose.End() Method stop the normal execution of the page, I have refatored your code and delete de Response.End() Method, now it's work.

    Response.Clear();
    Response.AppendHeader("content-disposition", "attachment; filename=RegionData.xlsm");
    Response.ContentType = "application/octet-stream";
    wbRegionData.Save(Response.OutputStream);//();
    Response.Redirect("RegionData.aspx?PALID=" + AVListID, true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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