0
votes

I'm trying to retrieve the list of images that a onenote page has. When I get the page content, an HTML, images have a source of the form:

https://www.onenote.com/api/beta/resources/{id}/$value

Doing a get over that url I get the following headers:

{
  'cache-control': 'no-cache',
  pragma: 'no-cache',
  'content-type': 'application/octet-stream',
  expires: '-1',
  server: 'Microsoft-IIS/8.0',
  'x-correlationid': '38283de4-110c-4c17-a371-c950c88025de',
  'x-usersessionid': '38283de4-110c-4c17-a371-c950c88025de',
  'x-officefe': 'OneNoteServiceFrontEnd_IN_2',
  'x-officeversion': '16.0.3429.1562',
  'x-officecluster': 'eus-www.onenote.com',
  p3p: 'CP="CAO DSP COR ADMa DEV CONi TELi CUR PSA PSD TAI IVDi OUR SAMi BUS DEM NAV STA UNI COM INT PHY ONL FIN PUR"',
  'x-content-type-options': 'nosniff',
  'request-processing-time': '31.2529 ms',
  'content-disposition': 'attachment',
  'preference-applied': 'odata.include-annotations=*',
  'x-aspnet-version': '4.0.30319',
  'x-powered-by': 'ASP.NET',
  date: 'Sat, 01 Nov 2014 00:17:25 GMT',
  'content-length': '10672' 
}

and the body of that response is a big binary.

I'm doing this with a Node Js app.

Could someone give me an sample of how to save the response into a image file, like a png or jpeg? Thanks in advance

1

1 Answers

0
votes

After some extra research I figure out how to do this.

Code:

downloadFile: function (url, fileName, callback)
	{
		var options = {
				url: url
			,	encoding: null
			,	headers:  {'Authorization': 'Bearer ' + this.context.accessToken}
			};

		request.get(options, function (e, httpResponse, body)
		{
			var buffer = new Buffer(body, 'binary');

			fs.writeFile(fileName, buffer, function(err)
			{
				if(err)
				{
					console.log(err);
					throw err;
				}
				else
				{
					callback()
				}
			});
		});
	}

The important part here is:

  1. encoding: null, otherwise request (the npm module) will treat your body as a string.
  2. You must know that OneNote treat all images as .emf, so or you convert them or save them in that file format.