
Untitled
By: a guest on
Aug 10th, 2012 | syntax:
None | size: 1.09 KB | hits: 7 | expires: Never
http.get with urls with spaces
function downloadFileFromURL( file_url, callback )
{
//-------------
// really complicated way to get a http.get save path
var protocol = url.parse( file_url).protocol;
var host = url.parse( file_url ).host;
var full_domain = protocol + '//' + host;
var escaped_path = escape(file_url.substring( full_domain.length ));
var options = {
host: host
, port: 80
, path: escaped_path
}
var file_url_info = url.parse( file_url );
var file_path = path.join( __dirname, 'images', path.basename(file_url) );
var request = http.get( options , function(res){
var imagedata = ''
res.setEncoding('binary')
res.on('data', function(chunk){
imagedata += chunk;
})
res.on('end', function(){
fs.writeFile( file_path, imagedata, 'binary', function(err){
if (err) callback( err );
else {
callback( null, file_path );
}
})
})
})
}
var encoded_url = encodeURI(file_url);