Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
196
rated 0 times [  200] [ 4]  / answers: 1 / hits: 16239  / 13 Years ago, mon, february 6, 2012, 12:00:00

I am getting data from a get request. The data (in the body of the response) looks something like this:



... ÿÀ����ÿÄ��������������ÿÄ�N��!1AQa2q¡#BR±ð3brS²ÁÂÑá$ñCDTst¢³&45dÃÒÿÄ������������ÿÄ�-������!1AQa¡ðq±ÁÑ2áÿÚ���?�û.                        R1º#ª¥7Jíî½M6îNö ]·!]=Fv­ß`7~qÆee²%·JokkZüCbìþ<ù{ã9öùË®´(%A,Ià�2I?t×bn6wÆù¥V 2SÀ><k5ºÙØ92EhÎçü¨/aÝ!ã|ñþ¥ñßT}U«¦ÒÚµ«xuÕfƳ KØ {ù{ð$·DúBMZÆcp}´R|Mä2ó8üg)·ùôfõ$zXiRÞü}óÆ>,êÚûíR5ý: .....


the response headers look like this:



HTTP/1.1 200 OK
Content-Length: 26965
Access-Control-Allow-Origin: *
Content-Type: image/jpeg; charset=UTF-8
Date: Mon, 06 Feb 2012 21:14:21 GMT
Expires: Mon, 06 Feb 2012 22:14:21 GMT
Cache-Control: public, max-age=3600
Last-Modified: Fri, 13 Feb 2009 23:31:30 GMT
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Server: Dropta Server 1.0
X-Frame-Options: SAMEORIGIN
Connection: close


I want to get the body content which is my image data and save it to a name.jpeg file on the server.



How can I do that? I tried using buffers combined with the fs module, but I am kind of lost.



Thanks


More From » node.js

 Answers
37

Here's an example, which downloads http://upload.wikimedia.org/wikipedia/commons/1/15/Jagdschloss_Granitz_4.jpg to name.jpeg



var fs=require('fs');
var http=require('http');

var f=fs.createWriteStream('name.jpeg');

var options={
host:'upload.wikimedia.org',
port:80,
path:'/wikipedia/commons/1/15/Jagdschloss_Granitz_4.jpg'
}

http.get(options,function(res){
res.on('data', function (chunk) {
f.write(chunk);
});
res.on('end',function(){
f.end();
});
});

[#87617] Saturday, February 4, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
travion

Total Points: 137
Total Questions: 96
Total Answers: 103

Location: India
Member since Wed, Aug 4, 2021
3 Years ago
travion questions
Mon, Dec 16, 19, 00:00, 5 Years ago
Sat, Oct 19, 19, 00:00, 5 Years ago
Fri, Sep 20, 19, 00:00, 5 Years ago
Wed, Nov 14, 18, 00:00, 6 Years ago
Sun, Oct 28, 18, 00:00, 6 Years ago
;