php - 将 dataurl 发布到 php
问题描述
ajax.open("POST", "upload.php", true);
ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//ajax.setRequestHeader("Content-Type", "image/png",1);
ajax.onreadystatechange = function() {
if(ajax.readyState == 4 && ajax.status == 200) {
alert(ajax.responseText);
}
}
ajax.send("imgData=" + canvasData);
}
这是将数据发送到upload.php 的xmlhttprequest,它表示无法保存文件。但是 php 页面将文件保存为 0B。
<?php
// Requires php5
define('UPLOAD_DIR', 'images/');
$img = $_POST['canvasData'];
print $img;
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
?>
以上是发布数据的php文件......
解决方案
如果您想将文件上传到目录使用move_uploaded_file
功能,但您的问题不清楚,请查看以下帖子链接
推荐阅读
- php - 如何通过电子邮件向每行的电子邮件地址发送新添加的 SQL 行?
- python - SQLAlchemy 查询按相关表中的字段值过滤
- matrix - 如何在 SAS 中初始化一个简单的矩阵?
- javascript - 如何以编程方式关闭 TimePickerAndroid
- docx4j - docx4j如何将图像插入表格单元格
- c++ - 如何知道 C2259 VS 2017 错误没有实现哪种方法?
- javascript - 将带有对象的数组转换为数组
- python - Boost Python Numpy - 未定义的初始化引用
- computer-vision - 从表面法线图像计算深度图像,对积分/求和算法感到困惑
- python - 在 npyscreen(Python 诅咒包装器)中应该如何调整小部件的大小?