【发布时间】:2015-04-10 11:34:11
【问题描述】:
在使用 Python 和来自 Dropbox 的 Python SDK 之前,我已经成功地做到了这一点,但现在我使用的是 Nodejs 和 Dropbox HTTP API,出现了问题。当我在本地保存 PDF 文件时,我只能看到原始 PDF 的一部分,当我将其与原始文件进行比较时,例如 WinMerge,我发现文件不相等并且大小不同。我能想到的唯一区别是它可能会使用与原始编码不同的编码进行保存,但我已经使用 iconv-lite 尝试了其中的大多数,但没有一个能给出好的结果。
我还尝试使用https://github.com/dropbox/dropbox-js 来查看它是否给出了不同的结果,还尝试使用https://www.npmjs.com/package/request 代替node-rest-client,但没有成功。
有没有人实现过这个并让它工作?
这是我的代码:
var fs = require('fs'),
RestClient = require('node-rest-client').Client;
var args = {
headers: {
'Content-Type': 'application/json',
'Accept': 'application/pdf',
'Authorization': 'Bearer xxx'
}
};
var restClient = new RestClient();
var url = 'https://api-content.dropbox.com/1/files/auto/' + dropboxPath;
var filePath = '/var/temp/' + fileName;
restClient.get(url, arguments, function(body, response) {
fs.writeFile(filePath, response, function (error, written, buffer) {});
}
当使用不同的编码进行测试时,它看起来像这样:
var fs = require('fs'),
RestClient = require('node-rest-client').Client,
iconvlite = require('iconv-lite');
iconvlite.extendNodeEncodings();
var args = {
headers: {
'Content-Type': 'application/json',
'Accept': 'application/pdf',
'Authorization': 'Bearer xxx'
}
};
var restClient = new RestClient();
var url = 'https://api-content.dropbox.com/1/files/auto/' + dropboxPath;
var filePath = '/var/temp/' + fileName;
var options = { encoding: 'UTF-8' };
restClient.get(url, arguments, function(body, response) {
fs.writeFile(filePath, response, options, function (error, written, buffer) {});
}
【问题讨论】:
标签: node.js pdf encoding dropbox-api