0

我正在尝试将画布保存为图像文件,用户可以确定要下载的图像格式(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";
        }
    }

该代码无法强制下载,我不知道它为什么不起作用。

非常感谢任何帮助。

4

1 回答 1

1

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

于 2012-08-15T12:53:40.257 回答