使用XMLHttpRequest上载在移动设备上不起作用

Upload using XMLHttpRequest not working on mobile

本文关键字:不起作用 移动 XMLHttpRequest 上载 使用      更新时间:2023-09-26

大家好,我目前正在开发一个使用XMLHttpRequest的上传功能。它在网络浏览器上运行得很好,但当我在手机上测试它时,它已经不起作用了。以下是在手机上选择照片后的错误日志:

E/chrome(2604):[错误:layer_tree_host_impl.cc(2121)]强制由于工作环境缺少,零拷贝磁贴初始化

我已经添加了人行横道

这是我在客户端上的代码:

if(fileInfo.size <= 20000000) {
    var xhr = new XMLHttpRequest();
    xhr.open('POST', '/uploadSomeWhere', true);
    xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
    if (xhr.upload) {
      xhr.upload.onprogress = function(e) {
        if (e.lengthComputable) {
          display.innerText = Math.floor((e.loaded / e.total) * 100) + '%';
          bootstrapPB.style.width = Math.floor((e.loaded / e.total) * 100) + '%';
        }
      }
      xhr.upload.onloadstart = function() {
        display.innerText = '0%';
        bootstrapPB.style.width = '0%';
      }
    }
    xhr.send(fileInfo);
} else {
  LocalState.set("mainError", "Please upload a file not more than 20MB");
}

在我的服务器中:

WebApp.connectHandlers.use('/uploadSomeWhere',function(req,res){
  function makeid()
  {
      var text = "";
      var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
      for( var i=0; i < 10; i++ )
          text += possible.charAt(Math.floor(Math.random() * possible.length));
      return text;
  }
  var uniquePhotoId = makeid();
  var originalPhoto = "/"+uniquePhotoId+"_original/";
  fs.mkdirSync("/home/dating/contents/"+originalPhoto);
  var file2 = fs.createWriteStream("/home/dating/contents"+originalPhoto+uniquePhotoId);
  file2.on('error',function(error){if(error){
    throw new Meteor.Error("Filed to upload image");
  }});
  file2.on('finish',function(){
      res.writeHead(200, {"content-type":"text/html"});
      res.end();
  });
  req.pipe(file2);
  // Set timeout to fully capture and convert the image
  setTimeout(function(){
   imageMagick(file2.path).autoOrient().setFormat("jpg").write(file2.path, function(){
     req.pipe(file2);
   });
  }, 1000);
  req._events.close;
});

请告知出了什么问题。非常感谢。顺便说一下,我正在使用ReactJS和Meteor创建一个混合移动应用程序。

我不确定xhr是否没有发送数据,或者服务器是否没有接受来自xhr 的发送数据

在reactjs中使用xhr似乎确实存在问题,所以我所做的是使用FileReader将图像从文件输入转换到缓冲区base64,然后从那里我使用服务器中的FS节点将缓冲区base64转换为图像,然后将其上传到目录。

现在一切都很好:)