<legend id='Kvaoo'><style id='Kvaoo'><dir id='Kvaoo'><q id='Kvaoo'></q></dir></style></legend>

<i id='Kvaoo'><tr id='Kvaoo'><dt id='Kvaoo'><q id='Kvaoo'><span id='Kvaoo'><b id='Kvaoo'><form id='Kvaoo'><ins id='Kvaoo'></ins><ul id='Kvaoo'></ul><sub id='Kvaoo'></sub></form><legend id='Kvaoo'></legend><bdo id='Kvaoo'><pre id='Kvaoo'><center id='Kvaoo'></center></pre></bdo></b><th id='Kvaoo'></th></span></q></dt></tr></i><div id='Kvaoo'><tfoot id='Kvaoo'></tfoot><dl id='Kvaoo'><fieldset id='Kvaoo'></fieldset></dl></div>

    1. <small id='Kvaoo'></small><noframes id='Kvaoo'>

        <bdo id='Kvaoo'></bdo><ul id='Kvaoo'></ul>
    2. <tfoot id='Kvaoo'></tfoot>

      从 Ajax 调用时文件下载脚本不起作用

      File download script doesn#39;t work when called from Ajax(从 Ajax 调用时文件下载脚本不起作用)
      <tfoot id='07xuL'></tfoot>
        <tbody id='07xuL'></tbody>

        • <bdo id='07xuL'></bdo><ul id='07xuL'></ul>

          <legend id='07xuL'><style id='07xuL'><dir id='07xuL'><q id='07xuL'></q></dir></style></legend>

          <small id='07xuL'></small><noframes id='07xuL'>

            <i id='07xuL'><tr id='07xuL'><dt id='07xuL'><q id='07xuL'><span id='07xuL'><b id='07xuL'><form id='07xuL'><ins id='07xuL'></ins><ul id='07xuL'></ul><sub id='07xuL'></sub></form><legend id='07xuL'></legend><bdo id='07xuL'><pre id='07xuL'><center id='07xuL'></center></pre></bdo></b><th id='07xuL'></th></span></q></dt></tr></i><div id='07xuL'><tfoot id='07xuL'></tfoot><dl id='07xuL'><fieldset id='07xuL'></fieldset></dl></div>

              • 本文介绍了从 Ajax 调用时文件下载脚本不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用以下脚本来启动文件下载:

                I'm using the following script to initiate file downloads:

                if (file_exists($newfilename)) {
                    header('Content-Description: File Transfer');
                    header('Content-Type: application/octet-stream');
                    header('Content-Disposition: attachment; filename='.basename($newfilename));
                    header('Content-Transfer-Encoding: binary');
                    header('Expires: 0');
                    header('Cache-Control: must-revalidate');
                    header('Pragma: public');
                    header('Content-Length: ' . filesize($newfilename));
                    ob_clean();
                    flush();
                    readfile($newfilename);
                    exit;
                }
                

                当我直接打开页面时它工作正常,但问题是,我需要从另一个页面通过 Ajax 调用此脚本.当我这样做时,下载不会开始.脚本的其余部分执行它应该做的事情.

                It works fine when I open the page directly, but the thing is, I need this script to be called through Ajax from another page. When I do that, then the download doesn't start. The rest of the script does what it's supposed to.

                我认为问题是无法以这种方式使用标头函数,但肯定有办法让它工作吗?

                I assume the problem is not being able to use the header function this way, but surely there's a way to make this work?

                如果有帮助的话,这是 Ajax 函数:

                This is the Ajax function if it's of any help:

                <script type="text/javascript">
                    // function create GetXmlHttpObject
                    function GetXmlHttpObject()
                    {
                    if (window.XMLHttpRequest)
                    {
                    // code for IE7+, Firefox, Chrome, Opera, Safari
                    return new XMLHttpRequest();
                    }
                    if (window.ActiveXObject)
                    {
                    // code for IE6, IE5
                    return new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    return null;
                    }
                
                    function submitVideoAjax(){
                    var myAjaxPostrequest=new GetXmlHttpObject();
                
                    var t2_title=document.video_form.title.value;
                
                    var parameters="title="+t2_title;
                
                    myAjaxPostrequest.open("POST", "newdownloadmanager.php", true);
                    myAjaxPostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                    myAjaxPostrequest.send(parameters);
                    myAjaxPostrequest.onreadystatechange=function(){
                    if(myAjaxPostrequest.readyState==4){
                    if(myAjaxPostrequest.status==200){
                    document.getElementById("result").innerHTML=myAjaxPostrequest.responseText;
                    document.getElementById("video_form").style.display = "none";
                
                    }
                    else    {
                    document.getElementById("video_form").innerHTML="An error has occured making the request";
                    }
                    }
                    }
                    }
                    </script>
                

                这是表格:

                <form name='video_form' id='video_form' method="post">
                <input type="hidden" name="title" id="title" value="Madelyn2-01.mp4"/>
                <button type="button" name="submit_video" id="submit_video" onclick="submitVideoAjax();">Download</button>
                </form>
                

                推荐答案

                您不能使用 AJAX 下载文件.这没有意义.您可以发送 AJAX 请求并在客户端的成功处理程序中获取文件内容,但出于明显的安全原因,您不能用它做太多事情.您无法将其保存在客户端计算机上,并且没有 javascript API 允许您提示用户将其保存到何处.

                You cannot use AJAX to download files. It doesn't make sense. You can send the AJAX request and fetch the file contents inside the success handler on the client, but for obvious security reasons you can't do much with it. You cannot save it on the client computer and there's no javascript API allowing you to prompt the user where to save it.

                所以要下载文件,不要使用 AJAX.创建一个指向您的服务器端脚本的锚点,用于提供要下载的文件.

                So to download files, don't use AJAX. Create an anchor pointing to your server side script that serves the file to be downloaded.

                这篇关于从 Ajax 调用时文件下载脚本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                <small id='BtN0a'></small><noframes id='BtN0a'>

                • <tfoot id='BtN0a'></tfoot>
                    <tbody id='BtN0a'></tbody>
                  <legend id='BtN0a'><style id='BtN0a'><dir id='BtN0a'><q id='BtN0a'></q></dir></style></legend>
                • <i id='BtN0a'><tr id='BtN0a'><dt id='BtN0a'><q id='BtN0a'><span id='BtN0a'><b id='BtN0a'><form id='BtN0a'><ins id='BtN0a'></ins><ul id='BtN0a'></ul><sub id='BtN0a'></sub></form><legend id='BtN0a'></legend><bdo id='BtN0a'><pre id='BtN0a'><center id='BtN0a'></center></pre></bdo></b><th id='BtN0a'></th></span></q></dt></tr></i><div id='BtN0a'><tfoot id='BtN0a'></tfoot><dl id='BtN0a'><fieldset id='BtN0a'></fieldset></dl></div>

                        • <bdo id='BtN0a'></bdo><ul id='BtN0a'></ul>