PHP generate file for download then redirect

前端 未结 11 2866
春和景丽
春和景丽 2020-11-22 09:08

I have a PHP app that creates a CSV file which is forced to download using headers. Here\'s the relevant part of the code:

header(\'Content-Type: applicatio         


        
11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 09:36

    very easy to do in the case it is really needed.

    But you will need to have a bit work in JavaScript and cookies:

    in PHP you should add setting up a cookie

    header('Set-Cookie: fileLoading=true'); 
    

    then on the page where you call the download you should track with JS (e.g. once per second) if there is coming cookie like that (there is used plugin jQuery cookie here):

    setInterval(function(){
      if ($.cookie("fileLoading")) {
        // clean the cookie for future downoads
        $.removeCookie("fileLoading");
    
        //redirect
        location.href = "/newpage";
      }
    },1000);
    

    Now if the file starts to be downoaded JS recognizes it and redirects to the page needed after cookie is deleted.

    Of course, you can tell you need browser to accept cookies, JavaScript and so on, but it works.

提交回复
热议问题