首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用XMLHttpRequest上传不工作在手机上

使用XMLHttpRequest上传不工作在手机上
EN

Stack Overflow用户
提问于 2016-07-26 04:58:08
回答 1查看 1.3K关注 0票数 0

嗨,伙计们,我目前正在开发一个使用XMLHttpRequest的上传功能。它在网络浏览器上工作得很好,但当我在手机上测试它时,它就不再工作了。以下是在移动平台上选择照片后的错误日志:

E/ as ( 2604):ERROR:layer_tree_host_impl.cc(2121)由于缺少工作上下文而强制零拷贝块初始化。

我已经加了人行横道

下面是我在客户机上的代码:

代码语言:javascript
复制
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");
}

在我的服务器上:

代码语言:javascript
复制
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的发送数据。

EN

回答 1

Stack Overflow用户

发布于 2016-07-27 09:17:43

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

现在一切都很好:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38581293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档