Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

javascript - Upload using XMLHttpRequest not working on mobile

Hi guys I am currently working on an Upload feature that uses 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

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.

I am not sure if the xhr is not sending data or the server is not accepting the send data from the xhr

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

Everything works great now :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...