Download a file and redirect it to another page via ajax

后端 未结 12 2250
盖世英雄少女心
盖世英雄少女心 2021-02-02 09:44

I have a simple contact form for download excel file . Main issue happen , When ajax load .I want to download excel file then redirect user to a next page.. Below is my code wit

相关标签:
12条回答
  • 2021-02-02 10:22

    In Ajax....Simply you can create an excel file on server ... ajax response will be the path of the excel file...On ajax success open the excel file in new tab (it automatically downloads excel) and at the same time open new tab with new location.

    sample code inside ajax success given below

      window.open("path to excel..");
        window.open("new location");
    

    also can use (if needed)

    window.location.replace("new location");
    

    Open multiple links in Chrome at once as new tabs

    0 讨论(0)
  • 2021-02-02 10:23

    I think it is very tough to download. Please Use PHP Excel to Create file and download.

    0 讨论(0)
  • 2021-02-02 10:28

    Try below method, hope it will work

    1. open ajaxexcel.php in new window via window.open
    2. it will start downloading, then close it.
    3. As soon as it closes, redirect page.
    0 讨论(0)
  • 2021-02-02 10:30

    You can't download file using Ajax request. Any how you have to redirect to particular location which will allow you to download file. Might be you tried with this, but try to download file in separate tab and redirect your page to particular page.

    <input id="btnGetResponse" type="button" value="Redirect" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
    $(function () {
        $("#btnGetResponse").click(function () {
            window.open('/test/excel.php');
            window.location = "http://stackoverflow.com/";
        });
    });
    </script>
    

    Also from your PHP file (excel.php) remove this line. Otherwise it will download your file as JPG or PNG

    header('Content-type: image/jpeg,image/gif,image/png');
    
    0 讨论(0)
  • 2021-02-02 10:35

    By using https://github.com/johnculviner/jquery.fileDownload

    Also from your PHP file (excel.php) remove this line. Otherwise it will download your file as JPG or PNG

    header('Content-type: image/jpeg,image/gif,image/png');

    0 讨论(0)
  • 2021-02-02 10:35

    Just use with simple javascript

    window.location.replace(###file url###);
    
    0 讨论(0)
提交回复
热议问题