2016-04-12 20 views
0

Bir android'den bir bayt dizisi alıp bir dosyaya dönüştürmeye çalışıyorum. Bunu nasıl yaparım? Ben de ekspres çerçeveyi kullanıyorum. Android uygulamanız itibarenNodejs'de byte dizisini nasıl alabilirim ve bir dosyaya dönüştürebilirim

+0

Daha fazla bilgi verebilir misiniz? Bayt dizisini bir api'ye mi gönderiyorsunuz? Şu anda hangi yaklaşımı alıyorsunuz? – RedJandal

+0

Hey, bir api'ye evet gönderiyorum. Sadece req.body.bytearray ve sonra fs.createwritestream (bitearray) yapabilir miyim diye merak ediyordum – QwertyKing

cevap

1

Seni ekspres kullandığınız varsayılmaktadır düğüm tarafında bu

String url = "http://yourserver/file-upload"; 
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), 
     "yourfile"); 
try { 
    HttpClient httpclient = new DefaultHttpClient(); 

    HttpPost httppost = new HttpPost(url); 

    InputStreamEntity reqEntity = new InputStreamEntity(
      new FileInputStream(file), -1); 
    reqEntity.setContentType("binary/octet-stream"); 
    reqEntity.setChunked(true); // Send in multiple parts if needed 
    httppost.setEntity(reqEntity); 
    HttpResponse response = httpclient.execute(httppost); 
    //Do something with response... 

} catch (Exception e) { 
    // show error 
} 

gibi bir şey yapacağız. Bir console.log (req.files) android app gönderme ne de bakabilirsiniz için mi bu

var fs = require('fs'); 
app.post('/file-upload', function(req, res) { 
    // get the temporary location of the file 
    var tmp_path = req.files.yourfile.path; 
    // set where the file should actually exists - in this case it is in the "images" directory 
    var target_path = './public/images/' + req.files.yourfile.name; 
    // move the file from the temporary location to the intended location 
    fs.rename(tmp_path, target_path, function(err) { 
     if (err) throw err; 
     // delete the temporary file, so that the explicitly set temporary upload dir does not get filled with unwanted files 
     fs.unlink(tmp_path, function() { 
      if (err) throw err; 
      res.send('File uploaded to: ' + target_path + ' - ' + req.files.yourfile.size + ' bytes'); 
     }); 
    }); 
}; 

gibi bir şey yapabilirsiniz. Req.files dosyasındaki "dosyanızı" değiştirin. Doğru özellik ile yourfile.

İlgili konular