2017-06-12 54 views
0

我想從android應用程序上傳文件,並希望將該數據寫入我們的服務器(保存文件到我們的服務器) 我可以寫入文件,但該圖像不會顯示從android應用程序寫入圖像數據到節點js服務器

我的代碼片段: -

var data=''; 
      req.setEncoding('binary'); // also try UTF8 
      req.on('data', function(chunk) { 
      data += chunk; 
      // f.write(chunk); 
      }); 
      req.on('end', function() { 
       let fileData = {}; 
       fileData.filename = filename; 
       fileData.fileextension = fileextension; 
       fs.writeFile(completeFilename, data, 'binary', function (err) { 
        if(err){ 
        console.log(err); 
        let response_data = status_codes.file_uplpoad_error; 
        console.log(response_data); 
        let enc = encrypt_decrypt.encrypt(cryptkey, iv, JSON.stringify(response_data)); 
        res.end(enc); 
        }else{ 
        let fileData = {}; 
        fileData.filename = filename; 
        fileData.fileextension = fileextension; 
        return callback(fileData); 
        } 
       }); 


}); 
req.on('error', function(err) { 
      // This prints the error message and stack trace to `stderr`. 
      console.error(err.stack); 
      console.log(err); 
      res.end('error'); 
      }); 

當我收到錯誤無法知道的一切工作wellexcept像的寫入enter image description here

已嘗試過所有的東西,但不能把它做 在此先感謝:)

回答

0

作爲建議:嘗試上載您的圖像在Base64而不是字節。通過這種方式,你不會面臨像你的很多問題

+0

所以從android我編碼的圖像數據爲base64,然後解碼從base64 –

+0

@VIKASKOHLI是的確切 – sasan

相關問題