php - 从 Ajax 中的 PHP 文件下载文件;文件不会下载
问题描述
我在表格中显示了一个文件库,每个文件都有复选框。目标是检查您要下载的文件,然后单击按钮下载包含这些单独文件的 zip 文件。
一切似乎都正常......数组,ajax,“成功”响应......但文件不会下载。是否可以通过这种方式下载文件?如果没有,我需要做些什么不同的事情才能正常工作?
jQuery
// searchIDs returns an array of file names. ie:
// 'file1.zip', 'file2.png', 'file3.pdf'
$('#toolkit-bin input[type=submit]').on('click', function(e) {
e.preventDefault();
$.ajax({
type : 'POST',
url : './functions.php',
data : { searchIDs: searchIDs },
success : function(data) {
alert(data);
},
error : function(request,error) {
alert("Request: "+JSON.stringify(request));
}
});
});
函数.php
// ROOT is defined as the root directory of the server.
$zipname = 'LiveLOUD-Toolkit.zip';
$zip = new ZipArchive();
$zip -> open($zipname, ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Get the array of the selected files.
$files = $_POST['searchIDs'];
// Loop through the files.
foreach ($files as $file)
{
// Get the path to the file.
$path = ROOT.'/_assets/toolkit/'.$file;
// Add the file to the zip.
$zip -> addFromString(basename($path), file_get_contents($path));
}
// Zip archive will be created only after closing object.
$zip -> close();
// If the file was created successfully...
if (file_exists($zipname))
{
// Download the zip file.
// THIS STUFF DOES NOT SEEM TO WORK
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: '.filesize($zipname));
// Delete the zip after download.
unlink($zipname);
// This is successfully returned.
echo 'Success!';
}
else
{
echo 'Error!';
}
解决方案
非常感谢味精和这个线程中的人。我通过在 functions.php 中创建 zip 文件并将文件名传回 ajax 来使我的下载工作。然后,使用window.location
打开拉链。我的最终代码如下:
jQuery/阿贾克斯:
// searchIDs returns an array of file names. ie:
// 'file1.zip', 'file2.png', 'file3.pdf'
// If submmit download button is clicked...
$('#toolkit-bin input[type=submit]').on('click', function(e) {
e.preventDefault();
var searchIDs = [];
$('#croud-toolkit').find(':checkbox:checked').map(function(){
searchIDs.push($(this).val());
});
$.ajax({
type : 'POST',
url : './functions.php',
data : { searchIDs: searchIDs },
success : function(data) {
window.location = './'+data;
},
error : function(data) {
alert('error');
}
});
});
函数.php
// ROOT is defined as the root directory of the server.
// Name and create the Zip archive.
$zipname = 'LiveLOUD-Toolkit.zip';
$zip = new ZipArchive();
$zip -> open($zipname, ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Get the array of the selected files.
$files = $_POST['searchIDs'];
// Loop through the files.
foreach ($files as $file)
{
// Get the path to the file.
$path = ROOT.'/_assets/toolkit/'.$file;
// Add the file to the zip.
$zip -> addFromString(basename($path), file_get_contents($path));
}
// Zip archive will be created only after closing object.
$zip -> close();
// Return data to Ajax
echo $zipname;
推荐阅读
- node.js - 如何使用node js实时更改内容?
- .net - 如何使用 Selenium Winium 在 WPF 中检查/查找元素以进行自动化测试
- django - Django DRF 将配置文件模型创建限制为经过身份验证的用户
- swift - Key-Value-Observing 闭包从不调用布尔属性
- javascript - 使用 document.getElementsByTagName 选择并更新列表中的选项
- node.js - 发生错误时 npm install 停止
- java - java:双==和双等于
- swift - 为什么执行 data.append(moredata) 时 swift 崩溃?或数据 = 数据1 + 数据2
- linux - 没有规则使目标 Cuda+Qt+Linux
- python - 使用 python 将 CSV 转换为 JSON 并使用分隔符“|”拆分 csv