Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 下载一个文件并通过ajax将其重定向到另一个页面_Jquery_Ajax_Codeigniter_Wiki - Fatal编程技术网

Jquery 下载一个文件并通过ajax将其重定向到另一个页面

Jquery 下载一个文件并通过ajax将其重定向到另一个页面,jquery,ajax,codeigniter,wiki,Jquery,Ajax,Codeigniter,Wiki,我有一个下载excel文件的简单联系方式。主要问题发生在加载ajax时。我想下载excel文件,然后将用户重定向到下一页。。下面是我的伪数据代码 Ajax代码 $.ajax({ type: "POST", url: "/site/ajaxexcel.php", data: {'value':'send'}, cache: false, success: function(html){ location.href = '<?php e

我有一个下载excel文件的简单联系方式。主要问题发生在加载ajax时。我想下载excel文件,然后将用户重定向到下一页。。下面是我的伪数据代码

Ajax代码

$.ajax({
    type: "POST",
    url: "/site/ajaxexcel.php", 
    data: {'value':'send'},
    cache: false,
    success: function(html){
        location.href = '<?php echo base_url()."/site/welcome.php" ?>';                 
    }
});
我只想下载excel文件,然后将用户重定向到特定位置


如果你做得好的话,你也可以帮我处理你的codeigniter代码。

你可以使用
jquery.fileDownload.js

你可以在这里找到一个例子

我觉得下载起来很难。请使用PHP Excel创建文件并下载。

实际上,对于这种情况,我建议使用空白选项打开文件位置并重定向。为此,您需要创建一个表单结构并将其提交到action.php

例如:

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);

试试下面的方法,希望能奏效

  • 通过
    窗口在新窗口中打开
    ajaxexcel.php
    。打开
  • 它将开始下载,然后关闭
  • 一旦它关闭,重定向页面

  • 您不能使用Ajax请求下载文件。任何您必须重定向到特定位置的方式都将允许您下载文件。您可能尝试过这个,但尝试在单独的选项卡中下载文件,并将页面重定向到特定页面

    <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>
    
    通过使用js:

    利用

    也可以从PHP文件(excel.PHP)中删除这一行。否则,它将以JPG或PNG格式下载您的文件

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

    标题('Content-type:image/jpeg、image/gif、image/png')

    您可以通过创建虚拟表单并发布来实现这一点

    function autoGenerateAndSubmitForm(method, url, post_data) {
        var element = document.getElementById("virtual_form");
        if(element != null )
        {
            element.parentNode.removeChild(element);
        }
        var form = document.createElement("form");
        form.setAttribute("id", "virtual_form");
        form.setAttribute("style", "display:none;");
        form.setAttribute("target", "_blank"); // This line is very important in your case for redirect in other page and download your file.
        form.method = method;
        form.action = url;   
        for(i in post_data)
        {
             var element=document.createElement("input");
             element.value=post_data[i];
             element.name=i;
             form.appendChild(element); 
        }
        document.body.appendChild(form);
        form.submit();
        form.parentNode.removeChild(form);
    }
    
    在您需要的地方调用上面的方法,我假设您已经在click事件中调用了它

    $('button').on('click', function(e){
         autoGenerateAndSubmitForm("POST","/site/ajaxexcel.php",{'value':'send'});
         location.href = '<?php echo base_url()."/site/welcome.php" ?>';     
    });
    

    在Ajax中…只需在服务器上创建excel文件。。。ajax响应将是excel文件的路径…ajax成功后,在新选项卡中打开excel文件(它会自动下载excel),同时打开新位置的新选项卡

    下面给出了ajax成功内部的示例代码

      window.open("path to excel..");
        window.open("new location");
    
    也可以使用(如果需要)


    方法1:使用jQuery AJAX

    用以下代码替换ajax

    <script>
    $.ajax({
        type: "POST",
        url: "/site/ajaxexcel.php", 
        data: {'value':'send'},
        cache: false,
        success: function(html)
        {
            window.open("/site/ajaxexcel.php");
            location.href = '<?php echo base_url()."/site/welcome.php" ?>';                 
        }
    });
    </script>   
    

    现在,当您单击anchor时,您的文件将被下载,您可以分别在
    done()/fail()
    函数中编写成功/失败代码。

    只需使用简单的javascript即可

    window.location.replace(###file url###);
    
    1) Ajax不是解决方案

    2)
    window.open()
    popup在大多数浏览器中都会被阻止,即使来自同一个域(至少在chrome和FF中发生过这种情况,当时我试图实现类似的功能)

    类似这样的东西可以完成这项工作:(它不会检查返回的代码之类的内容,因此
    404
    或非文件输出将导致在不下载的情况下重定向)

    document.getElementById('zzz')。onclick=function(){
    ifrm=document.createElement('iframe');
    ifrm.style.display=“无”;
    ifrm.src=”https://github.com/SheetJS/test_files/blob/b07031a7bd5a8b86b9baac94267f20cf81c6d5eb/A4X_2013.xls?raw=true"; 
    //来自github的一些随机xls文件(在单元格a-4中只包含一个“x”字符)
    ifrm.onload=函数(){
    window.location.href=”https://jquery.com";
    };
    文件.body.appendChild(ifrm);
    返回false;
    }


    它们没有完整的描述。我使用了他们的代码,但没有得到具体的结果。请给我写一个代码。我还想将用户重定向到另一个页面@Ravikiran kalal.Oh ya。。听起来不错。。谢谢,我已经试过了。但主要问题是重定向。下载代码后标头操作不起作用。请在PhpExcel$name=“file.xlsx”中使用此操作$objWriter->save('FolderName/'。$name)$path=“FolderName/file.xlsx”;标题(“位置:$path”);我明白你的意思。这对我来说不是很好..而且我想用ajax@vijju PundirI完成这项工作我不同意,我们可以使用php excel创建并下载文件,这是一个非常简单的过程。谢谢回答@shaymmakwana.me。。但这对我来说是不够的,也是不正确的答案。就像你能看到我的代码一样。。它们的头文件已用于下载文件。所以不能在这个文件中使用header:location代码。我想您误解了,您需要在父窗口(不是在ajaxexcel.php中)中编写重定向代码,从那里打开
    ajaxexcel.php
    。我建议再试一次,然后再回来。好的,让我再检查一下。非常感谢您的快速回复。我以我的方式尝试了您的代码。。我想,这不是我完全可以管理的。。请你帮我写代码好吗。提前谢谢,我想这个链接会帮助你感谢你的评论。实际上,他们这样说,使用外部js文件。但是我的ajax代码很简单,而且是计划好的。所以请帮我输入代码。@user1048123_SOreadytohelp@KumarRakesh你能把你的项目文件夹添加到我们可以下载的地方吗。Cz我需要更多的信息来帮助you@KumarRakesh有更新吗???@Spartan。。很抱歉更新太晚。。我的问题是。。我只有两个文件。。我已经用另一种方法把它修好了。。但我的回答还不够。。如果你能做到的话。请尝试使用我的代码..设置超时不是最佳方式。我想,10秒,少一点或多一点都不是正确的方法,亲爱的。我无法得到我的答案。它给了我一种解决方案。根据我的要求,这对我来说不太合适,我需要很多答案。。我得到了你的答案。。在这种情况下,我的文件不仅下载了位置href,而且像Ish答案一样工作。你能帮我写下你的完整代码吗?谢谢你的回复。我用过你的代码,但在控制台中发生了很多错误,比如阻止URL等。这对我来说是有效的。。。奇怪的是没有为你工作。。。。和wht块url?你在使用SSL证书吗?就像Milan malani的回答一样,我已经尝试过使用这种代码。。但主要问题是文件下载在新窗口中打开hii@kumarakesh,我同意文件下载将在新窗口中打开,但新窗口将自动关闭,下载对话框将立即出现,您将重定向到
    header('Content-type: image/jpeg,image/gif,image/png');
    
      window.open("path to excel..");
        window.open("new location");
    
    window.location.replace("new location");
    
    <script>
    $.ajax({
        type: "POST",
        url: "/site/ajaxexcel.php", 
        data: {'value':'send'},
        cache: false,
        success: function(html)
        {
            window.open("/site/ajaxexcel.php");
            location.href = '<?php echo base_url()."/site/welcome.php" ?>';                 
        }
    });
    </script>   
    
    <a class="download" href="/path/to/file">Download</a>
    <script>
    $(document).on("click", "a.download", function () {
                $.fileDownload($(this).prop('href'))
                    .done(function () { alert('File download a success!'); //success code})
                    .fail(function () { alert('File download failed!');//failure code });
    
                return false; //this is critical to stop the click event which will trigger a normal file download
            });
    </script>
    
    window.location.replace(###file url###);