2014-11-01 44 views
0

我试图检索一个onenote页面的图像列表。 当我得到的页面内容,一个HTML,图像有以下形式的来源:OneNote api下载图片

https://www.onenote.com/api/beta/resources/ {ID}/$值

做了这个URL,我得到以下标题的获取:

{ 
    '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' 
} 

和那个响应的主体是一个很大的二元组。

我正在做一个Node Js应用程序。

有人能给我一个如何将响应保存到图像文件中的示例,如png或jpeg吗? 在此先感谢

回答

0

经过一些额外的研究,我想出了如何做到这一点。

代码:

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

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

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

这里最重要的部分是:

  1. 编码:null,否则请求(NPM模块)会对待你的身体作为一个字符串。
  2. 您必须知道,OneNote将所有图像视为.emf,因此您可以将它们转换或保存为该文件格式。