首页 > 解决方案 > 将 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文件......

标签: phpajax

解决方案


如果您想将文件上传到目录使用move_uploaded_file功能,但您的问题不清楚,请查看以下帖子链接


推荐阅读