2013-02-08 2 views
0
function media(req,res){ 
    console.log(req.query.image); 
    var collectionName = 'imageTable'; 

     var selector = MongoHelper.idSelector(req.query.image); 
     MongoHelper.findOne(selector, collectionName, function(err, image) { 
      console.log(image.picture); 
      var url_parts = url.parse(image.picture); 

      var options = {host: url_parts.hostname, path: url_parts.pathname}; 

      http.get(options).on('response', function (response) { 
      var body = ''; 
      var i = 0; 
      response.on('data', function (chunk) { 
       i++; 
       body += chunk; 
       console.log('BODY Part: ' + i); 
      }); 
      response.on('end', function() { 

      console.log('Finished'); 
      res.writeHead(200,{'Content-Type':'image/JPEG'}); 
      res.write(body); 
      res.end(); 
     }); 
     }); 
     }); 
} 

다른 서버에서 이미지를 가져 오는 중입니다. 나는 그 이미지의 URL을 가지고있다. 그리고 나는 그 반응을 쓰고있다. 하지만 여기서 응답 이미지가 손상됩니다. 응답으로 jpeg 이미지를 작성하는 방법에 대한 아이디어?다른 서버에서 이미지를 읽고 응답을 작성하려고합니다.

답변

0
function media(req,res){ 
    console.log(req.query.image); 
    var collectionName = 'facebook'; 

     var selector = MongoHelper.idSelector(req.query.image); 
     MongoHelper.findOne(selector, collectionName, function(err, image) { 

      var url_parts = url.parse(image.picture); 
      var options = {host: url_parts.hostname, path: url_parts.pathname}; 

      http.get(options).on('response', function (response) { 
      res.writeHead(200,{'Content-Type':'image/JPEG'}); 
      response.on('data', function (chunk) { 
       res.write(chunk); 
      }); 
      response.on('end', function() { 
      res.end(); 
     }); 
     }); 
     }); 
} 

여기 해결책을 얻었습니다. 결국 전체 데이터를 쓰는 대신 파일 끝에 도달하면 응답을 받고 끝낼 때마다 기록하십시오. 그러나 아직도 누군가가 더 좋은 생각이 있다면 여기에 쓸 수 있습니다.