Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. var options = {
  2. host: 'google.com',
  3. port: 80,
  4. path: '/images/logos/ps_logo2.png'
  5. };
  6.  
  7. var request = http.get(options);
  8.  
  9. request.on('response', function (res) {
  10. res.on('data', function (chunk) {
  11. fs.writeFile(dir+'image.png', chunk, function (err) {
  12. if (err) throw err;
  13. console.log('It's saved!');
  14. });
  15. });
  16. });
  17.  
  18. var http = require('http')
  19. , fs = require('fs')
  20. , options
  21.  
  22. options = {
  23. host: 'www.google.com'
  24. , port: 80
  25. , path: '/images/logos/ps_logo2.png'
  26. }
  27.  
  28. var request = http.get(options, function(res){
  29. var imagedata = ''
  30. res.setEncoding('binary')
  31.  
  32. res.on('data', function(chunk){
  33. imagedata += chunk
  34. })
  35.  
  36. res.on('end', function(){
  37. fs.writeFile('logo.png', imagedata, 'binary', function(err){
  38. if (err) throw err
  39. console.log('File saved.')
  40. })
  41. })
  42.  
  43. })
  44.  
  45. var fs = require('fs');
  46. var request = require('request');
  47. // Or with cookies
  48. // var request = require('request').defaults({jar: true});
  49.  
  50. request.get({url: 'https://someurl/somefile.torrent', encoding: 'binary'}, function (err, response, body) {
  51. fs.writeFile("/tmp/test.torrent", body, 'binary', function(err) {
  52. if(err)
  53. console.log(err);
  54. else
  55. console.log("The file was saved!");
  56. });
  57. });
  58.  
  59. var http = require('http-request');
  60. var options = {url: 'http://localhost/foo.pdf'};
  61. http.get(options, '/path/to/foo.pdf', function (error, result) {
  62. if (error) {
  63. console.error(error);
  64. } else {
  65. console.log('File downloaded at: ' + result.file);
  66. }
  67. });
  68.  
  69. var http = require('http'),
  70. fs = require('fs'),
  71. options;
  72.  
  73. options = {
  74. host: 'www.google.com' ,
  75. port: 80,
  76. path: '/images/logos/ps_logo2.png'
  77. }
  78.  
  79. var request = http.get(options, function(res){
  80.  
  81. //var imagedata = ''
  82. //res.setEncoding('binary')
  83.  
  84. var chunks = [];
  85.  
  86. res.on('data', function(chunk){
  87.  
  88. //imagedata += chunk
  89. chunks.push(chunk)
  90.  
  91. })
  92.  
  93. res.on('end', function(){
  94.  
  95. //fs.writeFile('logo.png', imagedata, 'binary', function(err){
  96.  
  97. var buffer = Buffer.concat(chunks)
  98. fs.writeFile('logo.png', buffer, function(err){
  99. if (err) throw err
  100. console.log('File saved.')
  101. })
  102.  
  103. })
  104.  
  105. const subscriptionKey = 'your_azure_subscrition_key';
  106. const uriBase = // **MUST change your location (mine is 'eastus')**
  107. 'https://eastus.api.cognitive.microsoft.com/vision/v2.0/analyze';
  108.  
  109. // Request parameters.
  110. const params = {
  111. 'visualFeatures': 'Categories,Description,Adult,Faces',
  112. 'maxCandidates': '2',
  113. 'details': 'Celebrities,Landmarks',
  114. 'language': 'en'
  115. };
  116.  
  117. const options = {
  118. uri: uriBase,
  119. qs: params,
  120. body: fs.readFileSync(./my_local_image_path.jpg),
  121. headers: {
  122. 'Content-Type': 'application/octet-stream',
  123. 'Ocp-Apim-Subscription-Key' : subscriptionKey
  124. }
  125. };
  126.  
  127. request.post(options, (error, response, body) => {
  128. if (error) {
  129. console.log('Error: ', error);
  130. return;
  131. }
  132. let jsonString = JSON.stringify(JSON.parse(body), null, ' ');
  133. body = JSON.parse(body);
  134. if (body.code) // err
  135. {
  136. console.log("AZURE: " + body.message)
  137. }
  138.  
  139. console.log('Responsen' + jsonString);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement