javascript - SignaturePad 将签名保存到服务器
问题描述
我正在尝试将我的签名保存在服务器上,但它没有保存签名,但我在我的 js 控制台日志中获得了“data:image/png;base64,iVBORw ...”。请帮我在服务器上保存我的签名。
我的JS代码:
function uploadSignature(mimetype) {
var dataurl = signaturePad.toDataURL(mimetype);
var blobdata = dataURLtoBlob(dataurl);
var fd = new FormData(document.getElementById("UploadForm"));
fd.append("imageData", blobdata, "filename")
/** will result in normal file upload with post name "signature" on target url **/
$.ajax({
url: "/signature_pad.php",
type: 'POST',
data: fd,
processData: false,
contentType: false,
dataType: 'html',
success: function (response) {
alert("AJAX OK: uploadSignature() ok");
console.log(response);
},
error: function (e) {
alert("AJAX ERROR: uploadSignature() fehlgeschlagen");
console.log(e);
}
});
}
我的 PHP 代码将签名保存在服务器中:
<?php
if (isset($_POST['imageData'])) {
$imgData = base64_decode($_POST['imageData']);
$image_name= $_POST['image_name'];
// Path where the image is going to be saved
$filePath = '../'.$image_name.'.png';
// Delete previously uploaded image
if (file_exists($filePath)) { unlink($filePath); }
// Write $imgData into the image file
$file = fopen($filePath, 'w');
fwrite($file, $imgData);
fclose($file);
} else {
echo "imgData doesn't exists";
}
我是 ajax 新手,我不明白如何通过 ajax 发送请求以将签名保存在服务器上。太感谢了
解决方案
推荐阅读
- php - 如何在联接表中添加 count() 的位置?
- html - 如何禁用对文件夹 html 页面的直接访问?
- r - R:如何有效地为许多变量的二次形式生成 R 公式?
- javascript - 需要用 PHP 将属性的值更改为另一个值
- python - Tkinter:等待多个按钮中的任何一个被按下,然后删除所有按钮
- excel - 使用 VBA 将包含数据的新行添加到 Excel 工作表
- jenkins - 詹金斯 - 将参数传递给其他工作而不为自由式项目触发它
- django - 出于开发目的,如何在浏览器中打开带有初步渲染(扩展,包括)的 Django Jinjia2 模板?
- python - 从目录读取时更新图像
- javascript - 将函数添加到数组时的Javascript变量范围