简体   繁体   English

使用 XMLHttpRequest 上传无法在移动设备上运行

[英]Upload using XMLHttpRequest not working on mobile

Hi guys I am currently working on an Upload feature that uses XMLHttpRequest .大家好,我目前正在开发使用XMLHttpRequest的上传功能。 It works perfectly on web browsers but when I tested it on mobile it does not work anymore.它在网络浏览器上完美运行,但当我在移动设备上测试它时,它不再工作了。 Here is the error log after selecting photo on mobile:这是在手机上选择照片后的错误日志:

E/chromium( 2604): [ERROR:layer_tree_host_impl.cc(2121)] Forcing zero-copy tile initialization as worker context is missing E/chromium(2604):[错误:layer_tree_host_impl.cc(2121)] 由于缺少工作人员上下文,因此强制进行零拷贝切片初始化

I already added crosswalk我已经添加了人行横道

Here is my code on the client:这是我在客户端上的代码:

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");
}

In my server:在我的服务器中:

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;
});

Please advise what went wrong.请指教出了什么问题。 Thanks a lot.非常感谢。 By the way I am creating a hybrid mobile app using ReactJS and Meteor.顺便说一句,我正在使用 ReactJS 和 Meteor 创建一个混合移动应用程序。

I am not sure if the xhr is not sending data or the server is not accepting the send data from the xhr我不确定 xhr 是否没有发送数据或服务器不接受来自 xhr 的发送数据

It seems there is really an issue using xhr in reactjs so what I did to sort things out is use FileReader to convert image from file input to buffer base64 and from there I use FS node in the server to convert the buffer base64 to image then upload it to the directory.似乎在 reactjs 中使用 xhr 确实存在问题,所以我要做的就是使用 FileReader 将图像从文件输入转换为缓冲区 base64,然后我使用服务器中的 FS 节点将缓冲区 base64 转换为图像然后上传它到目录。

Everything works great now :)现在一切都很好:)

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

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