Guest User

Untitled

a guest
Aug 10th, 2012
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. http.get with urls with spaces
  2. function downloadFileFromURL( file_url, callback )
  3. {
  4. //-------------
  5. // really complicated way to get a http.get save path
  6. var protocol = url.parse( file_url).protocol;
  7. var host = url.parse( file_url ).host;
  8. var full_domain = protocol + '//' + host;
  9. var escaped_path = escape(file_url.substring( full_domain.length ));
  10.  
  11. var options = {
  12. host: host
  13. , port: 80
  14. , path: escaped_path
  15. }
  16.  
  17. var file_url_info = url.parse( file_url );
  18. var file_path = path.join( __dirname, 'images', path.basename(file_url) );
  19.  
  20. var request = http.get( options , function(res){
  21.  
  22. var imagedata = ''
  23. res.setEncoding('binary')
  24.  
  25. res.on('data', function(chunk){
  26. imagedata += chunk;
  27. })
  28.  
  29. res.on('end', function(){
  30.  
  31. fs.writeFile( file_path, imagedata, 'binary', function(err){
  32. if (err) callback( err );
  33. else {
  34. callback( null, file_path );
  35. }
  36. })
  37. })
  38.  
  39. })
  40. }
  41.  
  42. var encoded_url = encodeURI(file_url);
Advertisement
Add Comment
Please, Sign In to add comment