Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将画布图像另存为图像文件并强制下载-无法打开下载对话框_Php_Ajax_Html_Canvas_Force Download - Fatal编程技术网

Php 将画布图像另存为图像文件并强制下载-无法打开下载对话框

Php 将画布图像另存为图像文件并强制下载-无法打开下载对话框,php,ajax,html,canvas,force-download,Php,Ajax,Html,Canvas,Force Download,我试图将画布保存为图像文件,用户可以确定要下载的图像格式(png或jpg),然后强制下载该文件,而无需将该文件存储在服务器上 到目前为止,我得到的是: JS脚本: $.ajax( { type : "POST", url : "../php/download_image.php", data: { format: 'png', dataURL: flattenCanvas.toDataURL('image/png') } }); PHP: 代码不能强制下

我试图将画布保存为图像文件,用户可以确定要下载的图像格式(png或jpg),然后强制下载该文件,而无需将该文件存储在服务器上

到目前为止,我得到的是:

JS脚本:

$.ajax(
{
type : "POST",
    url : "../php/download_image.php",
data:
    {
    format: 'png',
    dataURL: flattenCanvas.toDataURL('image/png')
    }
});
PHP:

代码不能强制下载,我不知道为什么它不工作


非常感谢您的帮助

如果不想将文件存储在服务器上,则不需要以任何方式与服务器交互。只需让用户按说明下载画布的内容。

文件似乎已完美地写入服务器/.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";
        }
    }