Ror*_*ory 8 html php ajax jquery
在我的HTML页面上,我向一个强制下载的php脚本发出了一个JQuery ajax请求,但什么也没发生?
在我的html页面上(在链接的点击事件处理程序中)......
var file = "uploads/test.css";
$.ajax(
{
type : "POST",
url : "utils/Download_File.php",
data : {"file":file}
})
Run Code Online (Sandbox Code Playgroud)
Download_File.php脚本如下所示
<?php
Download_File::download();
class Download_File
{
public static function download()
{
$file = $_POST['file'];
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment');
readfile('http://localhost/myapp/' . $file);
exit;
}
}
?>
Run Code Online (Sandbox Code Playgroud)
但是由于某种原因什么都没发生?我查看了firebug中的响应头,无法看到任何问题.我正在使用Xampp.任何帮助深表感谢.
谢谢!
你应该指定Content-Transfer-Encoding
.另外,你应该指定filename
你的Content-Disposition
.
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="'.$file.'"');
readfile('http://localhost/myapp/'.$file);
exit;
Run Code Online (Sandbox Code Playgroud)
filename
因为RFC 2231要求使用双引号很重要.已知Firefox在下载文件名中包含空格的文件时遇到问题(如果filename
不在引号中).
此外,请检查以确保关闭后没有空白区域?>
.如果在关闭PHP标记后甚至存在空格,则标题将不会发送到浏览器.
作为旁注,如果您要提供许多常见文件类型供下载,则可以考虑指定这些MIME类型.这为最终用户提供了更好的体验.例如,你可以这样做:
//Handles the MIME type of common files
$extension = explode('.', $file);
$extension = $extension[count($extension)-1];
SWITCH($extension) {
case 'dmg':
header('Content-Type: application/octet-stream');
break;
case 'exe':
header('Content-Type: application/exe');
break;
case 'pdf':
header('Content-Type: application/pdf');
break;
case 'sit':
header('Content-Type: application/x-stuffit');
break;
case 'zip':
header('Content-Type: application/zip');
break;
default:
header('Content-Type: application/force-download');
break;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4449 次 |
最近记录: |