Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
Advertisement
Add Comment
Please, Sign In to add comment