Download a file and redirect it to another page via ajax

后端 未结 12 2245
盖世英雄少女心
盖世英雄少女心 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:20

    Actually for this situation i recommend open file location with blank option and redirect. For this purpose you need create a form structure and submit it to your action.php

    Example:

    var form = document.createElement("form");
    form.setAttribute("method", "post");
    form.setAttribute("action", "action.php");
    form.setAttribute("target", "myView");
    // or you can use _blank : form.setAttribute("target", "_blank");
    
    var hiddenField = document.createElement("input"); 
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "message");
    hiddenField.setAttribute("value", "val");
    form.appendChild(hiddenField);
    document.body.appendChild(form);
    window.open('', 'myView');
    form.submit();
    alert("Redirect in 10 second!");
    setTimeout(function(){
      location.href = "/home"
    }, 10000);
    

提交回复
热议问题