javascript - Base64上传图片问题

标签 javascript php jquery base64 common-lisp

抱歉问了这么长的问题,但我想尽我所能地表达它,并以易于理解的方式提出它。我有一个程序,允许用户在 JavaScript 中使用croppie.js 裁剪图像,并将图像发送到运行 Lisp 程序的后端的 Hunchentoot 服务器。用户上传 Base64 图像后,将其保存到 .png 文件时遇到问题。将 post 请求发送到服务器后,我将获取 Base64 图像作为字符串,通过创建没有 post 请求发送的标题的字符串子序列,并用“%”字符替换 Base64 请求中的无效字符。 “+”字符使 Base64 有效。接下来,我删除字符串末尾的子字符串 +3D+3D,因为我在 Common Lisp 中使用的 s-base64 库提示 +3D​​+3D 无效填充,我将其替换为“==”,这被认为是有效的填充。接下来,我使用 s-base64 库创建一个字节数组,将 Base64 字符串转换为字节数组,并将其存储在变量中。然后我循环遍历字节数组并将每个字节写入输出文件。完成后,我决定将字节数组的末尾打印到控制台,以便我可以查看输出和结束填充是否有效(看起来是有效的)。这是代码的一部分,带有注释以使其更清晰:

(define-easy-handler (handle-image :uri "/handle-image.html") ()
    (let ((data-source (hunchentoot:raw-post-data :force-text t))) ;get Base64 string
      (let ((new-string (subseq data-source 36))) ;create a subsequence of Base64 string
         (let ((final-string (substitute #\+ #\% new-string))) ;substitute % for +
            (let ((end (search "+3D+3D" final-string))) ;find the invalid padding
                (setf final-string (concatenate 'string (subseq final-string 0 end) "==")) ;add valid padding
                (let ((byte-array (with-input-from-string (in final-string) ;create byte array (or simple-vector) out of Base64 string
                                     (decode-base64-bytes in))))
                   (with-open-file (out "path/path/path/path/profile-image.png" ;create output stream to store file
                                         :direction :output
                                         :if-exists :supersede
                                         :element-type 'unsigned-byte)
                      (dotimes (i (length byte-array)) ;write each byte from the byte array to output stream
                        (write-byte (aref byte-array i) out)))) ;close stream
                (format t "!!!!!!!!: ~a" (subseq final-string (- (length final-string) 30))))))) ;print ending to console to ensure proper padding
       "Upload Successful") ;send response to client

这是我的一些 JavaScript 代码:

$(document).ready(function(){

     $image_crop = $('#image_demo').croppie({
        enableExif: true,
        viewport: {
          width:200,
          height:200,
          type:'square' //circle
        },
        boundary:{
          width:300,
          height:300
        }
   });

如您所见,我首先创建了裁剪器。我允许用户裁剪 200 x 200 的正方形,裁剪空间的总大小为 300 x 300。这部分代码没有问题:

$('#upload_image').on('change', function(){
var reader = new FileReader();
reader.onload = function (event) {
  $image_crop.croppie('bind', {
    url: event.target.result
  }).then(function(){
    console.log('jQuery bind complete');
  });
}

上面,当他们选择文件时,我将他们上传到裁剪器的图像绑定(bind)(就像您上传 Facebook 图像一样)。再次强调,没有问题:

 reader.readAsDataURL(this.files[0]);
$('#uploadImageModal').modal('show');

在上面,我读取了他们选择的文件,然后模式“弹出”,就像您正在裁剪 Facebook 或 Instagram 照片一样:

$('.crop_image').click(function(event){
       $image_crop.croppie('result', {
             type: 'canvas',
             size: 'viewport'
        }).then(function(response){
               $.ajax({
                    url:"handle-image.html",
                    type: "POST",
                    data:{"image": response},
                    success:function(data){
                            $('#uploadImageModal').modal('hide');
                            $('#uploaded_image').html(data);
                     }
              });
          })
       });

上面我上传了ajax请求,如果上传成功,他们会从服务器收到一条消息,表明上传成功,并且我还隐藏了图像裁剪的模式。

现在的问题是图像完全是空白的。我知道 Base64 是有效的,因为我使用 Base64 转换工具来查看该字符串是否有效。我还回去对图像的位、字节、像素和尺寸进行了研究,以了解计算机如何与它们交互,所以我不确定为什么我的图像只是显示空白。下面是我网站上的裁剪器的外观:

enter image description here

绑定(bind)正在工作,然后我收到上传成功的消息。但是在写入图像并在文件系统中查看它之后,它要么是空白图像,要么有时会说不支持该图像类型。

我在这篇文章中标记 PHP 的原因是因为我确信有些人在 PHP 中通过 ajax 上传裁剪图像时遇到过类似的问题,其中一些解决方案可能适用于这种情况,但显然需要我将解决方案翻译成 Lisp 语法。我的假设是,当我将字符串转换为字节数组并将其写入文件时,我的代码出现了问题,但我认为如果我忽略了某些内容,最好发布代码的其他部分。

最佳答案

正如 Brad 评论的那样,您应该首先尝试直接使用二进制上传。

除此之外:如果您在 Base64 编码的字符串中遇到 %,则很可能意味着整个内容另外进行了 URL 编码。快速的 apropos 搜索给出了 do-urlencode 作为解码它的库。将 % 替换为 + 可以使 base64 有效,但结果不一定代表有效的 jpg。

另外:使用 let* 而不是嵌套的 let 形式。也许使用write-sequence而不是按字节输出。

关于javascript - Base64上传图片问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59232055/

相关文章:

php - 未定义的命名空间 html(将 css 文件添加到 blade)laravel

javascript - 如何通过jQuery获取动态表中的行索引

jquery - 如何使用 jQuery 从 <p> 获取前 2 行或 200 个字符

javascript - "error"或 "fail"用于 jQuery 中的 $.post Ajax 调用

javascript - 导航链接功能故障

php - 如何使用 php 从 mysql 推送 latin-2 字符

javascript - Jquery rpg游戏逻辑并将数据推送到html

javascript - 添加 Bootstrap 5 搜索栏下拉菜单

javascript - Gulp 有 2 个相互冲突的任务

php - Magento PHP fatal error : Class 'XXXXXX' not found in Mage. php 第 516 行