【问题标题】:Save canvas image as image file and force download - fail to open download dialog将画布图像保存为图像文件并强制下载 - 无法打开下载对话框
【发布时间】: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


【解决方案1】:

如果您不想将文件存储在服务器上,则无需以任何方式与服务器交互。只需让用户按照here 的描述下载画布的内容即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    • 2018-11-15
    • 2011-12-12
    • 1970-01-01
    • 2013-12-07
    • 2015-06-15
    • 1970-01-01
    相关资源
    最近更新 更多