简体   繁体   English

如何使用JavaScript中的Ajax调用将数组中存储的多个base64图像发送到Java

[英]How to send multiple base64 images stored in array to Java using ajax call in JavaScript

I allow the user to upload multiple images at once. 我允许用户一次上传多张图片。

I store all image sources in an array and send with ajax. 我将所有图像源存储在一个数组中,并使用ajax发送。

The ajax call returns an error message that the size is too long. Ajax调用返回一条错误消息,即大小过长。

My code snippet : 我的代码段:

function save_domain_images(base64ImageSrc){
  var datastring = "&token="+domainImgSaveSession+"&imgSrc="+base64ImageSrc+"&action=saveDomainImg";
  $.ajax({
    type : "POST",
    url : base+"/AppUserListServiceProvider",
    data : datastring,
    cache : false,
    success : function(data){
      data                      = JSON.parse(data);
      domainImgSaveSession      = data["randomNew"];
      if(data["error"] == null && data["croppedImg"] != null){
        console.log("data : "+data["croppedImg"]);
      }
    },
    error : function(){}
  });
}
const match = /^\s*data:([\w/]+)(?:;base64)?,/.exec(base64Str);
const type = match[1];
const blob = b64toBlob(url.slice(match[0].length), type);
const formData = new FormData();
formData.append('image', blob, name);

function b64toBlob(base64) {
  const code = window.atob(base64.split(",")[1])
  const aBuffer = new window.ArrayBuffer(code.length)
  const uBuffer = new window.Uint8Array(aBuffer)

  for (let i = 0; i < code.length; i++) {
    uBuffer[i] = code.charCodeAt(i)
  }

  const Builder = window.WebKitBlobBuilder || window.MozBlobBuilder
  if (Builder) {
    const builder = new Builder()
    builder.append(buffer)

    return builder.getBlob(format)
  } else {
    return new window.Blob([buffer], {
      type: format
    })
  }
}

then you can post the formData 然后您可以发布formData

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM