我从 GitHub 存储库中获得了此函数,该函数将 Canvas 上绘制的签名输出到 DataURL()。当我检查它时,它以一串编码数据(a .png)的形式返回。
用户单击保存按钮,会发生以下情况:
saveButton.addEventListener("click", function (event) {
if (signaturePad.isEmpty()) {
alert("Please provide signature first.");
} else {
saveSignature(signaturePad.toDataURL());
}
});
function saveSignature(dataURL) {
$.ajax({
type: "POST",
datatype: "json",
url: "script.php",
data: {
imgBase64: dataURL
}
}).done(function(o) {
console.log('saved');
});
signaturePad.clear();
}
然后它会触发同一文件夹中的 PHP 脚本,名为 script.php
。
<?php
// requires php5
define('UPLOAD_DIR', 'images');
$img = $_POST['imgBase64'];
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
?>
我不知道为什么它会导致 500 服务器错误。
控制台没有打印“无法保存文件”,也没有打印“已保存”。
最佳答案
您在script.php
中有错误您正在 $img
中接收数据使用$data
写入时所以需要替换变量
<?php
// requires php5
define('UPLOAD_DIR', 'images');
$img = $_POST['imgBase64'];
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $img); //<---- change here
print $success ? $file : 'Unable to save the file.';
?>
关于javascript - 使用 PHP 在服务器上保存 .toDataURL() 错误 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44612052/