简体   繁体   English

在 iOS 上使用 Blob 图像的 FormData、XMLHttpRequest

[英]FormData, XMLHttpRequest with blob image on iOS

I'm trying to convert pasted images (blobs) in contentEditable elements to 'proper' images by uploading them.我正在尝试通过上传将contentEditable元素中粘贴的图像(blob)转换为“正确的”图像。

My code for getting the images and uploading them.我用于获取图像并上传它们的代码。

function convertimages(article)
{
  images = article.getElementsByTagName('img');
  for (var i = 0; i < images.length; i++) {
    if(images[i].src.startsWith('blob:')){

      formData = new FormData();

      fetch(images[i].src)
        .then(function(response) {
          return response.blob()
        })
        .then(function(blob) {
          formData.append("image", blob );
        });

      xmlhttp=new XMLHttpRequest();
      xmlhttp.open("POST","image.php",false);

      xmlhttp.send(formData);
      images[i].src = xmlhttp.responseText;
    }
  }
}

And my code for handling the upload:还有我处理上传的代码:

//create unique ID for filename
$id = uniqid();
$dest = './images/kip'.$id;

if (move_uploaded_file ( $_FILES['image']['tmp_name'], $dest )){
  echo $dest;
} else {
  echo "error.jpg";
}

But when inspecting $_FILES , it is an empty array.但是在检查$_FILES ,它是一个空数组。

Promises..Promises承诺..承诺

async function convertimages(article)
{
  images = article.getElementsByTagName('img');
  for (var i = 0; i < images.length; i++) {
    if(images[i].src.startsWith('blob:')){

      formData = new FormData();

      await fetch(images[i].src)
        .then(function(response) {
          return response.blob()
        })
        .then(function(blob) {
          formData.append("image", blob );
        }).then(function(){
          xmlhttp=new XMLHttpRequest();
          xmlhttp.open("POST","image.php",false);
          xmlhttp.send(formData);
          images[i].src = xmlhttp.responseText;
    });
    }
  }
}

My first experience with them我与他们的第一次经历

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

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