【发布时间】:2020-12-07 15:39:03
【问题描述】:
我有一个网络应用程序,它允许用户使用网络摄像头拍照,然后将其显示为 HTML 画布。然后画布成功地转换为图像,显示出来,然后使用 XMLHttpRequest 发送到服务器。这个过程发生在以下JS函数被触发时:
function convertCanvasToImage() {
let canvas = document.getElementById("canvas");
let image = new Image();
document.getElementById("profile").src = canvas.toDataURL();
var newName = canvas.toDataURL();
xhr = new XMLHttpRequest();
xhr.open('POST', 'saveimage.php');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(encodeURI('imgBase64=' + newName));
}
PHP 代码 saveimage.php 然后处理要保存到服务器的图像:
define('UPLOAD_DIR', 'uploads/new.png');
$img = $_POST['imgBase64'];
$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.';
此过程在本地服务器 (XAMPP) 上完全按预期工作,但在 IIS(Windows Server 2019 上的 IIS10)上运行时,我收到“无法保存文件”。消息。
我假设这是 IIS 的安全问题,但不确定解决方案是否涉及 PHP 标头、IIS 中的 CORS 或完全其他问题。有什么建议吗?
【问题讨论】:
-
保存前检查目录权限或输出结果
-
谢谢,编辑目录权限解决了问题
标签: javascript php iis xmlhttprequest