Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

node-XMLHttpRequest, it seems, does not support binary downloads - see this issue. What you are seeing is the file's binary contents converted into String which, in JavaScript, is an irreversible and destructive process for binary data (which means you cannot convert the string back to buffer and get the same data as the original contents).

Using request, you can download a binary file this way:

var request = require('request') , fs = require('fs') request.get( { url: 'your-file-url' , encoding: null // Force Request to return the data as Buffer , headers: { Authorization: 'Bearer ' + accessTokenHere } } , function done (err, res) { // If all is well, the file will be at res.body (buffer) fs.writeFile('./myfile.docx', res.body, function (err) { // Handle err somehow // Do other work necessary to finish the request }) } ) 

Note: This will buffer the whole file into memory before it can be saved to disk. For small files, this is fine, but for larger files, you might want to look into implementing this as a streamed download. This SO questionThis SO question already answers that, I recommend you have a look.

More information about how to authorize your requests can be found on Google Developers docs.

node-XMLHttpRequest, it seems, does not support binary downloads - see this issue. What you are seeing is the file's binary contents converted into String which, in JavaScript, is an irreversible and destructive process for binary data (which means you cannot convert the string back to buffer and get the same data as the original contents).

Using request, you can download a binary file this way:

var request = require('request') , fs = require('fs') request.get( { url: 'your-file-url' , encoding: null // Force Request to return the data as Buffer , headers: { Authorization: 'Bearer ' + accessTokenHere } } , function done (err, res) { // If all is well, the file will be at res.body (buffer) fs.writeFile('./myfile.docx', res.body, function (err) { // Handle err somehow // Do other work necessary to finish the request }) } ) 

Note: This will buffer the whole file into memory before it can be saved to disk. For small files, this is fine, but for larger files, you might want to look into implementing this as a streamed download. This SO question already answers that, I recommend you have a look.

More information about how to authorize your requests can be found on Google Developers docs.

node-XMLHttpRequest, it seems, does not support binary downloads - see this issue. What you are seeing is the file's binary contents converted into String which, in JavaScript, is an irreversible and destructive process for binary data (which means you cannot convert the string back to buffer and get the same data as the original contents).

Using request, you can download a binary file this way:

var request = require('request') , fs = require('fs') request.get( { url: 'your-file-url' , encoding: null // Force Request to return the data as Buffer , headers: { Authorization: 'Bearer ' + accessTokenHere } } , function done (err, res) { // If all is well, the file will be at res.body (buffer) fs.writeFile('./myfile.docx', res.body, function (err) { // Handle err somehow // Do other work necessary to finish the request }) } ) 

Note: This will buffer the whole file into memory before it can be saved to disk. For small files, this is fine, but for larger files, you might want to look into implementing this as a streamed download. This SO question already answers that, I recommend you have a look.

More information about how to authorize your requests can be found on Google Developers docs.

Bounty Awarded with 100 reputation awarded by Harpreet Singh
Removed fs.writeFileSync() and used its async counterpart
Source Link
Robert Rossmann
  • 12.1k
  • 4
  • 45
  • 74

node-XMLHttpRequest, it seems, does not support binary downloads - see this issue. What you are seeing is the file's binary contents converted into String which, in JavaScript, is an irreversible and destructive process for binary data (which means you cannot convert the string back to buffer and get the same data as the original contents).

Using request, you can download a binary file this way:

var request = require('request') , fs = require('fs') request.get( { url: 'your-file-url' , encoding: null // Force Request to return the data as Buffer , headers: { Authorization: 'Bearer ' + accessTokenHere } } , function done (err, res) { // If all is well, the file will be at res.body (buffer) /fs.writeFile('./ PSmyfile. Dodocx', NOTres.body, usefunction *Sync(err) in{  production! // Handle err somehow fs.writeFileSync('. /myfile.docx',/ res.bodyDo other work necessary to finish the request }) } ) 

Note: This will buffer the whole file into memory before it can be saved to disk. For small files, this is fine, but for larger files, you might want to look into implementing this as a streamed download. This SO question already answers that, I recommend you have a look.

More information about how to authorize your requests can be found on Google Developers docs.

node-XMLHttpRequest, it seems, does not support binary downloads - see this issue. What you are seeing is the file's binary contents converted into String which, in JavaScript, is an irreversible and destructive process for binary data (which means you cannot convert the string back to buffer and get the same data as the original contents).

Using request, you can download a binary file this way:

var request = require('request') , fs = require('fs') request.get( { url: 'your-file-url' , encoding: null // Force Request to return the data as Buffer , headers: { Authorization: 'Bearer ' + accessTokenHere } } , function done (err, res) { // If all is well, the file will be at res.body (buffer) // PS. Do NOT use *Sync() in production! fs.writeFileSync('./myfile.docx', res.body) } ) 

Note: This will buffer the whole file into memory before it can be saved to disk. For small files, this is fine, but for larger files, you might want to look into implementing this as a streamed download. This SO question already answers that, I recommend you have a look.

More information about how to authorize your requests can be found on Google Developers docs.

node-XMLHttpRequest, it seems, does not support binary downloads - see this issue. What you are seeing is the file's binary contents converted into String which, in JavaScript, is an irreversible and destructive process for binary data (which means you cannot convert the string back to buffer and get the same data as the original contents).

Using request, you can download a binary file this way:

var request = require('request') , fs = require('fs') request.get( { url: 'your-file-url' , encoding: null // Force Request to return the data as Buffer , headers: { Authorization: 'Bearer ' + accessTokenHere } } , function done (err, res) { // If all is well, the file will be at res.body (buffer) fs.writeFile('./myfile.docx', res.body, function (err) {   // Handle err somehow  // Do other work necessary to finish the request }) } ) 

Note: This will buffer the whole file into memory before it can be saved to disk. For small files, this is fine, but for larger files, you might want to look into implementing this as a streamed download. This SO question already answers that, I recommend you have a look.

More information about how to authorize your requests can be found on Google Developers docs.

Updated with authorization example
Source Link
Robert Rossmann
  • 12.1k
  • 4
  • 45
  • 74

node-XMLHttpRequest, it seems, does not support binary downloads - see this issue. What you are seeing is the file's binary contents converted into String which, in JavaScript, is an irreversible and destructive process for binary data (which means you cannot convert the string back to buffer and get the same data as the original contents).

Using request, you can download a binary file this way:

var request = require('request') , fs = require('fs') request.get( { url: 'your-file-url' , encoding: null // Force Request to return the data as Buffer , headers: { Authorization: 'Bearer ' + accessTokenHere } } , function done (err, res) { // If all is well, the file will be at res.body (buffer) // PS. Do NOT use *Sync() in production! fs.writeFileSync('./myfile.docx', res.body) } ) 

Note: This example does not include authentication - this you will need to figure out yourself.

Note 2: This will buffer the whole file into memory before it can be saved to disk. For small files, this is fine, but for larger files, you might want to look into implementing this as a streamed download. This SO question already answers that, I recommend you have a look.

More information about how to authorize your requests can be found on Google Developers docs.

node-XMLHttpRequest, it seems, does not support binary downloads - see this issue. What you are seeing is the file's binary contents converted into String which, in JavaScript, is an irreversible and destructive process for binary data (which means you cannot convert the string back to buffer and get the same data as the original contents).

Using request, you can download a binary file this way:

var request = require('request') , fs = require('fs') request.get( { url: 'your-file-url' , encoding: null // Force Request to return the data as Buffer } , function done (err, res) { // If all is well, the file will be at res.body (buffer) // PS. Do NOT use *Sync() in production! fs.writeFileSync('./myfile.docx', res.body) } ) 

Note: This example does not include authentication - this you will need to figure out yourself.

Note 2: This will buffer the whole file into memory before it can be saved to disk. For small files, this is fine, but for larger files, you might want to look into implementing this as a streamed download. This SO question already answers that, I recommend you have a look.

node-XMLHttpRequest, it seems, does not support binary downloads - see this issue. What you are seeing is the file's binary contents converted into String which, in JavaScript, is an irreversible and destructive process for binary data (which means you cannot convert the string back to buffer and get the same data as the original contents).

Using request, you can download a binary file this way:

var request = require('request') , fs = require('fs') request.get( { url: 'your-file-url' , encoding: null // Force Request to return the data as Buffer , headers: { Authorization: 'Bearer ' + accessTokenHere } } , function done (err, res) { // If all is well, the file will be at res.body (buffer) // PS. Do NOT use *Sync() in production! fs.writeFileSync('./myfile.docx', res.body) } ) 

Note: This will buffer the whole file into memory before it can be saved to disk. For small files, this is fine, but for larger files, you might want to look into implementing this as a streamed download. This SO question already answers that, I recommend you have a look.

More information about how to authorize your requests can be found on Google Developers docs.

Source Link
Robert Rossmann
  • 12.1k
  • 4
  • 45
  • 74
Loading