问题描述
在此处发布是因为我尚未在Dropbox论坛上收到回复. Dropbox论坛帖子
posting here because I've not received a response over at the dropbox forums. dropbox forum post
几个月前,我实现了保管箱选择器,并使一切正常运行.然后大约一周前,我遇到了一个问题,其中.pdf文件可以打开,但包含空白页,而.docx文件根本无法打开(.rtf和.txt文件可以很好打开)
A few months ago I implemented the dropbox chooser and got everything working fine. Then about a week ago I ran into an issue where .pdf files would open, but contain blank pages and .docx files won't open at all (.rtf and .txt files open just fine)
无论如何,我开始进行挖掘,结果发现返回的xhr.responseText短了多个字节(在pdf文件中大约短了4%)
Anyway I started digging and it turns out that the xhr.responseText that comes back is short a number of bytes (approximately 4% short in pdf files)
我看了看字节,文件的开头和结尾是正确的(不同版本存在相同的问题):
I've taken a look at the bytes and the beginning and end of file are correct (different versions have the same issue):
%PDF-1.5
...
%% EOF
%PDF-1.5
...
%%EOF
以下是相关代码:
var options = {
success: function(files)
{
var xhr = new XMLHttpRequest();
xhr.open("GET", files[0].link, false);
xhr.responseType = 'arrayBuffer';
xhr.onreadystatechange = function(oEvent)
{
if (xhr.readyState === 4 && xhr.status === 200)
{
var buffer = xhr.responseText;
console.log('File size: ' + files[0].bytes +
' | Buffer size: ' + buffer.length +
' | Header size: ' + xhr.getResponseHeader('Content-Length'));
}
}
xhr.setRequestHeader("User-Agent", navigator.userAgent);
try
{
xhr.send(null);
}
catch (err)
{
alert(err);
}
},
linkType: "direct", // or "preview"
multiselect: false, // or true
};
Dropbox.choose(options);
在控制台中,文件大小"和标题大小"的值相同,但是缓冲区大小",responseText的值较小...
In the console the "File size" and "Header size" values are the same, but the "Buffer size", the responseText, it smaller...
为什么保管箱会返回所有文件内容?
Why is dropbox returning all of the file contents?
推荐答案
我认为问题是您正在从XMLHttpRequest
的responseText
字段而不是response
读取.我的猜测是,这会导致(错误地)尝试将数据解释为文本字符.
I think the issue is that you're reading from the XMLHttpRequest
's responseText
field instead of response
. My guess would be that this results in (incorrectly) trying to interpret the data as text characters.
编辑:您还需要将arrayBuffer
更改为arraybuffer
才能真正获得ArrayBuffer
.然后,您需要检查byteLength
字段以获取ArrayBuffer
中包含的实际字节数.
You also need to change arrayBuffer
to arraybuffer
to actually get an ArrayBuffer
back. Then you need to check the byteLength
field to get the actual number of bytes contained in the ArrayBuffer
.
请注意,要使用arraybuffer
响应类型,您需要切换为使用异步XMLHttpRequest
. (将false
最后一个参数拖放到xhr.open
或将其值更改为true
.)
Note that to use the arraybuffer
response type, you'll need to switch to using an asynchronous XMLHttpRequest
. (Drop the false
last parameter to xhr.open
or change its value to true
.)
(对我而言)有效的示例位于 https://downloadtest.site44.com .代码如下:
A working (for me) example is at https://downloadtest.site44.com. The code is as follows:
Dropbox.choose({
success: function (files) {
var xhr = new XMLHttpRequest();
xhr.open('GET', files[0].link);
xhr.responseType = 'arraybuffer';
xhr.onreadystatechange = function (oEvent) {
if (xhr.readyState === 4 && xhr.status === 200) {
var buffer = xhr.response;
console.log('File size: ' + files[0].bytes +
' | Buffer size: ' + buffer.byteLength +
' | Header size: ' + xhr.getResponseHeader('Content-Length'));
}
};
xhr.send();
},
linkType: 'direct'
});
这篇关于Dropbox选择器返回损坏的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!