PHP AJAX Jquery - 文件下载问题

标签 php jquery ajax file download

Possible Duplicate:
Downloading Via JQuery AJAX Post not working

filedownload.php 有以下片段。

$file = 'cut.png';
header("Content-Type: image/png");
header('Content-Disposition: attachment; filename="'.$file.'"');
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
readfile($file);
exit();

AJAX 调用

jQuery.post('filedownload.php',{            
  'file'  :  result  // not used for the time being                    
 });

我对 filedownload.php 进行了 ajax 调用文件。它不允许用户下载该文件。但如果我直接运行 php,它允许用户下载文件。可能是什么问题 ?

我想使用核心功能而不是使用 jQuery 插件。如果不可能的话,插件就可以了。

鉴于我使用ajax,因为页面无法刷新。

最佳答案

The Problem

Let's take the example of a productivity web app such as a spreadsheet editor, which has the ability to open, save, import and export. The open and save options would involve loading a spreadsheet from the database, whereas import and export deal with local files on the user's machine. To implement the export behavior, you might decide that the user should have to save their spreadsheet first, allowing you to export the data from the backend to file. But let's assume instead you'd like to allow users to export their data without saving, perhaps to afford them the option of working locally without ever storing data on the server. In order to do this, you'd need to send the current spreadsheet data to the backend and receive a file to download. Unfortunately, this can not be handled using Ajax, since Ajax can only receive responses in the form of text. In cases where the data to be saved is rather lengthy, this poses a considerable problem.

The Workaround

In order to make the request, you'd need to make a regular (not Ajax) HTTP request using GET or POST. If the data is reasonably short, you might get away with a GET request (perhaps by simply setting Window.location to your export url), but due to varying browser limitations on GET request length, a POST will most likely be needed. The following plugin allows you to make a request that returns a file in a similar syntax to jQuery's native Ajax functions.

修复问题的 jQuery 代码

jQuery.download = function(url, data, method){
    //url and data options required
    if( url && data ){ 
        //data can be string of parameters or array/object
        data = typeof data == 'string' ? data : jQuery.param(data);
        //split params into form inputs
        var inputs = '';
        jQuery.each(data.split('&'), function(){ 
            var pair = this.split('=');
            inputs+='<input type="hidden" name="'+ pair[0] +'" value="'+ pair[1] +'" />'; 
        });
        //send request
        jQuery('<form action="'+ url +'" method="'+ (method||'post') +'">'+inputs+'</form>')
        .appendTo('body').submit().remove();
    };
};

如何调用

$.download('filedownload.php','filename='+filename );

Read more

关于PHP AJAX Jquery - 文件下载问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13874063/

相关文章:

html - 在ajax中用jquery更新css?

php - wp_localize_script $data 数组中的参数

php - MySQL - 忽略插入错误 : duplicate entry

php - SELECT 语句在 mariadb 10.0 中不起作用,但在 mariadb 5.5 中起作用

jquery - 选择第一个没有 "display:none"的元素

javascript - 内容 slider - JQuery 问题

php - 在php中循环生成动态表

php - 从 WooCommerce 中的特定产品属性术语名称获取产品

jquery - jCrop:处理不同的图像尺寸

javascript - AJAX 刷新延迟/不一致