从阿贾克斯调用时文件下载脚本不工作 [英] File download script doesn't work when called from Ajax

查看:99
本文介绍了从阿贾克斯调用时文件下载脚本不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的脚本来启动文件下载:

 如果(file_exists($ newfilename)){
    标题(内容简介:文件传输);
    标题(内容类型:应用程序/八位字节流);
    标题(内容处置:附件;文件名='基本名($ newfilename));
    标题(内容传输编码:二进制');
    头('过期:0');
    标题(缓存控制:必重新验证');
    头('杂注:公开');
    标题(内容长度:文件大小($ newfilename));
    ob_clean();
    冲洗();
    ReadFile的($ newfilename);
    出口;
}
 

它工作正常,当我直接打开网页,但事情是,我需要这个脚本通过阿贾克斯从另一个页面调用。当我这样做,那么下载没有启动。脚本的其余部分做什么它应该。

我认为这个问题是不能够使用的头功能这种方式,但肯定有一种方法,使这项工作?

这是Ajax的功能,如果它的任何帮助:

 <脚本类型=文/ JavaScript的>
    //函数创建GetXmlHttpObject
    功能GetXmlHttpObject()
    {
    如果(window.XMLHtt prequest)
    {
    // $ C $下IE7 +,火狐,Chrome,歌剧,Safari浏览器
    返回新XMLHtt prequest();
    }
    如果(window.ActiveXObject)
    {
    // code对IE6,IE5
    返回新的ActiveXObject(Microsoft.XMLHTTP);
    }
    返回null;
    }

    功能submitVideoAjax(){
    VAR myAjaxPostrequest =新GetXmlHttpObject();

    VAR t2_title = document.video_form.title.value;

    VAR参数=标题=+ t2_title;

    myAjaxPostrequest.open(POST,newdownloadmanager.php,真正的);
    myAjaxPostrequest.setRequestHeader(内容型,应用程序/ x-WWW的形式urlen codeD);
    myAjaxPostrequest.send(参数);
    myAjaxPostrequest.onreadystatechange =功能(){
    如果(myAjaxPostrequest.readyState == 4){
    如果(myAjaxPostrequest.status == 200){
    的document.getElementById(结果)的innerHTML = myAjaxPostrequest.responseText。
    。的document.getElementById(video_form)的style.display =none的;

    }
    其他    {
    的document.getElementById(video_form)的innerHTML =发生错误提出请求。
    }
    }
    }
    }
    < / SCRIPT>
 

这是格式如下:

 <表格名称='video_form'ID ='video_form'方法=后>
<输入类型=隐藏名称=称号ID =标题值=Madelyn2-01.mp4/>
<按钮式=按钮NAME =submit_videoID =submit_video的onclick =submitVideoAjax();>下载< /按钮>
< /形式GT;
 

解决方案

您不能使用AJAX来下载文件。它没有意义。您可以发送AJAX请求并获取客户端的成功处理程序中的文件内容,但显而易见的安全原因,你不能用它做很多。你不能将它保存在客户端计算机上,有没有JavaScript的API,让您提示用户保存它。

所以要下载的文件,不要使用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;
}

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?

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>

And this is the form:

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

解决方案

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.

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

这篇关于从阿贾克斯调用时文件下载脚本不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆