使用JavaScript从Dropbox下载文件 [英] Download file from Dropbox with JavaScript
问题描述
我有一个网站,试图向客户提供一项服务,以便从Dropbox下载文件。为了简化开发,我使用 Dropbox选择器。
为此,我启用了我希望从其中下载的域,并包括Dropbox本身建议的< script>
标签(以及相应的 data-app -key
)进入我的HTML页面。
一切正常。
I have a web site were try to provide a service to a client to be abel to download from Dropbox a file. For simplicity of development I use Dropbox chooser.
For this I enable domains I expect to download from and include <script>
tag suggested by Dropbox itself (with corresponding data-app-key
) into my HTML page.
Everything works sweet.
现在,我需要下载用户选择的文件。 Dropbox选择器似乎没有为此提供任何功能,它只是在检索有关文件的信息。就我而言,这是一个直接链接
,用于下载文件。
Now I need to download a file selected by the user. Dropbox chooser doesn't seem to provide any functionality for this, what it does, is just retrieve an information about file. In my case this is a direct link
, to download the file.
要下载文件,在我看来,我需要使用 Dropbox.Client
,它是在 // cdnjs.cloudflare.com/ajax/libs/dropbox.js/0.9.1/dropbox.min.js
To download the file, seems to me, I need to use Dropbox.Client
which is defined in another Dropbox javascript library at //cdnjs.cloudflare.com/ajax/libs/dropbox.js/0.9.1/dropbox.min.js
因此,使用该libarry运行以下代码:
So using that libarry I run the code like this:
//OPTIONS FOR DROPBOX CHOOSER
var options = {
linkType: "direct",
// THIS FUNCITON RUNS WHEN USER SELECTS SOMETHING
// FROM DOPBOX_CHOOSER
success: function (files) {
// DEFINE APP KET FOR DROPBOX_CLIENT (KEY, SECRET...), WHICH I GET
// BY CREATING NEW "CORE API" TYPE:Full Dropbox APPLICATION ON
// DROPBOX APP CONSOLE
var appKey = { key: 'APP KEY', secret: 'CLIENT SECRET', sandbox: true };
//INIT CLIENT
var client = new Dropbox.Client(appKey);
//TRY TO AUTHENTICATE IT
client.authenticate(function (error, client) {
if (error) {
console.log(error);
}
if (client.isAuthenticated()) {
//READ FILES
for (var i = 0; i < files.length; i++) {
var file = files[i];
client.readFile(file.link, function (error, data) {
if (error) {
return console.log(error); // Something went wrong.
}
alert(data); // data has the file's contents
});
}
} else {
console.log("Error on authentication");
}
});
},
cancel: function () {
}
};
//OPEN DROPBOX_CHOOSER
Dropbox.choose(options);
但是所有这些都无法向我报告:
But all this fails reporting me:
如果我请勿致电 client.authenticate
我无法下载文件,因为收到未授权错误通知。
If I don't call client.authenticate
I'm not able to download file as get "Not Authorized error" notification.
如何解决此问题。 ?
推荐答案
一个简单明了的解决方案是使用XMLHTTP,如下所示:
A simple and straightforward solution is using XMLHTTP as follows
function readDropbox(sURL)
{
var oRequest = new XMLHttpRequest();
oRequest.open("GET",sURL,false);
oRequest.onreadystatechange = function(oEvent)
{
if (oRequest.readyState === 4)
{
if (oRequest.status === 200)
{
console.log(oRequest.responseText)
}
else
{
console.log("Error", oRequest.statusText);
}
}
}
oRequest.setRequestHeader("User-Agent",navigator.userAgent);
try
{
oRequest.send(null)
}
catch (err)
{
alert(err);
}
if (oRequest.status == 200)
{
return oRequest.responseText;
}
else
{
alert("Error - File not found in Dropbox public folder");
return null;
}
}
这篇关于使用JavaScript从Dropbox下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!