【问题标题】:Upload on server image html2canvas上传服务器图像 html2canvas
【发布时间】:2019-07-25 14:51:19
【问题描述】:

我有这个js:

$.ajax({
   url: 'ajaxfile.php',
   type: 'POST',
   data: {
        image: base64URL
   },
   success: function(data){
       console.log(data);
       $.notify("info", "Upload successfully");
   },
   error: function () {
       $.notify("Error on image upload");
   }
});

PHP 代码:

<?php
  $image = $_POST['image'];
  $location = "src/upload/";
  $image_parts = explode(";base64,", $image);
  $image_base64 = base64_decode($image_parts[1]);
  $filename = "screenshot_".uniqid().'.png';
  $file = $location . $filename;
  file_put_contents($file, $image_base64);
  return [
     'status' => true
  ]
?>

调用已完成(我在浏览器控制台中看到),但在 console.log 上我返回了代码 php。似乎什么也没发生,代码php没有实现。你有想法吗?提前谢谢,对不起我的英语

我放了一张有错误的图片

【问题讨论】:

    标签: php jquery jquery-ui nginx html2canvas


    【解决方案1】:

    file_put_contents() 在失败时返回false,因此您可以将其分配给一个变量并使用它来确定您的状态,如下所示:

    <?php
        $image = $_POST['image'];
        $location = "src/upload/";
        $image_parts = explode(";base64,", $image);
        $image_base64 = base64_decode($image_parts[1]);
        $filename = "screenshot_".uniqid().'.png';
        $file = $location . $filename;
    
        $imageData = file_put_contents($file, $image_base64);
    
        if ($imageData !== false) {
            echo "success";
        } else {
            http_response_code(500);
            die();
        }
    ?>
    

    【讨论】:

    • @HareaCostea 你是什么意思你“得到所有的 php 代码”?屏幕截图显示了一个 XML 解析错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-26
    • 2023-03-10
    • 2016-03-21
    • 1970-01-01
    • 2017-06-22
    • 2013-12-17
    • 2014-03-21
    相关资源
    最近更新 更多