javascript -从html2canvas保存或保存为对话框时的默认文件名


javascript - Default filename when saving from html2canvas or save as dialogue?

我的一个朋友为我配置了h2ml2canvas,因为我不懂javascript。当使用h2ml2canvas保存时,它会生成一个随机文件名,例如

df0e604b2962492165eb8f2b31578171

是否有办法指定文件名前缀?例如,足球然后生成一个随机的3-4位数字?或者,是否有一种方法可以打开另存为对话框,而不是点击下载图像?我的download.php文件

<?php
$file = trim($_GET['path']);
// force user to download the image
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));
    ob_clean();
    flush();
    readfile($file);
    unlink($file);
    exit;
}
else {
    echo "error not found";
}
?>

您的文件名实际上是由PHP服务器端生成(或不是)的,而不是您引用的JavaScript。当它返回要发送回的数据时,它包含一个Content-Disposition标头,可能看起来像这样:

Content-Disposition: attachment

可以通过在header中添加:

向浏览器建议文件名
Content-Disposition: attachment; filename=soccer123.xyz

在PHP的某个地方,你应该找到:

header("Content-Disposition", "attachment");

或类似。你可以把它改成:

header("Content-Disposition", "attachment; filename=soccer-" . rand(100,999) . ".xyz");

(可能最好将.xyz作为图像类型的适当扩展名,例如.png.jpg…)


重新编辑,您可以替换:

header('Content-Disposition: attachment; filename='.basename($file));

header('Content-Disposition: attachment; filename=soccer-'.rand(100,999).'.xyz');

你需要一个正确的扩展名,而不是.xyz