【发布时间】:2012-08-15 12:11:04
【问题描述】:
我正在尝试将画布保存为图像文件,用户可以确定要下载哪种图像格式(png 或 jpg),然后强制下载文件而不将文件存储在服务器上。
这是我目前得到的:
JS 脚本:
$.ajax(
{
type : "POST",
url : "../php/download_image.php",
data:
{
format: 'png',
dataURL: flattenCanvas.toDataURL('image/png')
}
});
PHP:
$data = $_POST['dataURL'];
$format = $_POST['format'];
$file = $file = md5(uniqid()) . '.'.$format;
$uri = substr($data,strpos($data,",")+1);
file_put_contents($file, base64_decode($uri));
if($format == 'png')
{
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
else {
echo "$file not found";
}
}
代码无法强制下载,我不知道为什么它不起作用。
非常感谢任何帮助。
【问题讨论】:
-
看来文件已经完美写入服务器/..php/文件夹了...
标签: php ajax html canvas force-download