2016-07-26 19 views
0

嗨,大家好我目前正致力於使用XMLHttpRequest的上傳功能。它在Web瀏覽器上完美工作,但是當我在移動設備上測試它時,它不再工作。這裏是移動選擇照片後,錯誤日誌:使用XMLHttpRequest上傳不適用於手機

E/chromium(2604): [ERROR:layer_tree_host_impl.cc(2121)] Forcing zero-copy tile initialization as worker context is missing

我已經添加了人行橫道

這是我在客戶端代碼:

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 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 

     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創建混合移動應用程序。

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

+0

我嘗試添加代碼'xhr.onreadystatechange =函數(){ 如果(xhr.readyState === 4){ 如果(xhr.status === 200) console.log('successful'); } else { console.log('failed'); } } }' 查看'xhr.send()'是否成功發送,它確實等於200,因此它正確執行。但是沒有在手機上上傳 –

+0

我不確定xhr是否沒有發送數據,或者服務器不接受xhr的發送數據。 –

回答

0

看來真的是有在reactjs使用XHR所以我做了什麼的東西出來時使用的FileReader轉換圖像從文件輸入緩衝的base64,並從那裏我用FS節點的服務器轉換進行排序的問題然後將緩衝區base64映像上傳到目錄。

一切都很正常,現在:)