使用 Dropbox API V2 + Cordova 上传文件到 Dropbox
Uploading Files to Dropbox using Dropbox API V2 + Corodva
有没有人能够使用 Javascript Dropbox (Link to Dropbox javascript SDK) API V2 in Cordova Application? I had a look at the Dropbox-sdk.js file for method to upload files but all the methods require content of the file we want to upload to dropbox More about Upload methods here SDK 将文件上传到 Dropbox。我们如何提供文件的内容?
examples from the Javascript Sdk 使用输入类型文件元素获取要上传到 Dropbox 的文件。但是在 Cordova 的情况下该怎么做呢?我们如何传递文件的内容?
下面是我将文件上传到 Dropbox 的代码,但是当我尝试打开上传的文件时,它显示没有内容的 pdf 文件。
function uploadFile(tmpStrListStr)
{
var tmpStrList = "";
var uploadSuccess = false;
tmpStrList = tmpStrListStr.substring(0, tmpStrListStr.length-1).split(",");
istrue = true;
for(var i = 0 ; i < tmpStrList.length; i++)
{
var path = cordova.file.externalRootDirectory+'/Test/Logs/'+tmpStrList[i] + '.pdf';
window.resolveLocalFileSystemURL(path, function (fileEntry) {
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onloadend = function(e) {
var ACCESS_TOKEN = localStorage.accessToken;
var dbx = new Dropbox({ accessToken: ACCESS_TOKEN });
var fileCommitInfo = {};
fileCommitInfo.contents = reader.result;
fileCommitInfo.path = '/' + fileEntry.name;
fileCommitInfo.mode = { '.tag': 'overwrite' };
fileCommitInfo.autorename = true;
fileCommitInfo.mute = true;
dbx.filesUpload(fileCommitInfo)
.then(function(response) {
alert(response);
})
.catch(function(errr) {
console.log(errr);
});
}
reader.readAsDataURL(file);
});
}, function (e) {
console.log("FileSystem Error");
console.dir(e);
});
}
}
有没有其他方法可以在不使用 Javascript SDK 的情况下为 Cordova 应用程序实现 Dropbox 功能(API V2)?
全世界有没有人可以告诉我如何使用 Javascript SDK V2 将文件上传到 Dropbox?
要读取文件内容,请使用 XMLHttpRequest。根据响应,创建一个 blob 对象,然后将其设置为 FilesUpload 方法的内容参数。
function UploadNewFile() {
var rawFile = new XMLHttpRequest();
rawFile.responseType = 'arraybuffer';
rawFile.open("GET", "Your file Path Here", true);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
var blobObj = new Blob([rawFile.response],{ type: 'application/pdf',endings: 'native' });
dbx = new Dropbox({accessToken: "Your Access Token"});
if (dbx != null) {
dbx.filesUpload({
path:'/' + "File Name Here"+ '.pdf',
contents: blobObj,
mode: 'overwrite',
mute: true
}).then(function (response) {
var showmsg = "File Upload Complete";
reset();
alertify.alert(showmsg, function (e)
{
if (e)
{
//Code to be executed after your files are successfully uploaded to Dropbox.
}
});
}
}).catch(function (error) {
var showmsg = "Error saving file to your Dropbox!";
reset();
alertify.alert(showmsg);
});
};
}
}
}
rawFile.send(null);
}
有没有人能够使用 Javascript Dropbox (Link to Dropbox javascript SDK) API V2 in Cordova Application? I had a look at the Dropbox-sdk.js file for method to upload files but all the methods require content of the file we want to upload to dropbox More about Upload methods here SDK 将文件上传到 Dropbox。我们如何提供文件的内容? examples from the Javascript Sdk 使用输入类型文件元素获取要上传到 Dropbox 的文件。但是在 Cordova 的情况下该怎么做呢?我们如何传递文件的内容?
下面是我将文件上传到 Dropbox 的代码,但是当我尝试打开上传的文件时,它显示没有内容的 pdf 文件。
function uploadFile(tmpStrListStr)
{
var tmpStrList = "";
var uploadSuccess = false;
tmpStrList = tmpStrListStr.substring(0, tmpStrListStr.length-1).split(",");
istrue = true;
for(var i = 0 ; i < tmpStrList.length; i++)
{
var path = cordova.file.externalRootDirectory+'/Test/Logs/'+tmpStrList[i] + '.pdf';
window.resolveLocalFileSystemURL(path, function (fileEntry) {
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onloadend = function(e) {
var ACCESS_TOKEN = localStorage.accessToken;
var dbx = new Dropbox({ accessToken: ACCESS_TOKEN });
var fileCommitInfo = {};
fileCommitInfo.contents = reader.result;
fileCommitInfo.path = '/' + fileEntry.name;
fileCommitInfo.mode = { '.tag': 'overwrite' };
fileCommitInfo.autorename = true;
fileCommitInfo.mute = true;
dbx.filesUpload(fileCommitInfo)
.then(function(response) {
alert(response);
})
.catch(function(errr) {
console.log(errr);
});
}
reader.readAsDataURL(file);
});
}, function (e) {
console.log("FileSystem Error");
console.dir(e);
});
}
}
有没有其他方法可以在不使用 Javascript SDK 的情况下为 Cordova 应用程序实现 Dropbox 功能(API V2)?
全世界有没有人可以告诉我如何使用 Javascript SDK V2 将文件上传到 Dropbox?
要读取文件内容,请使用 XMLHttpRequest。根据响应,创建一个 blob 对象,然后将其设置为 FilesUpload 方法的内容参数。
function UploadNewFile() {
var rawFile = new XMLHttpRequest();
rawFile.responseType = 'arraybuffer';
rawFile.open("GET", "Your file Path Here", true);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
var blobObj = new Blob([rawFile.response],{ type: 'application/pdf',endings: 'native' });
dbx = new Dropbox({accessToken: "Your Access Token"});
if (dbx != null) {
dbx.filesUpload({
path:'/' + "File Name Here"+ '.pdf',
contents: blobObj,
mode: 'overwrite',
mute: true
}).then(function (response) {
var showmsg = "File Upload Complete";
reset();
alertify.alert(showmsg, function (e)
{
if (e)
{
//Code to be executed after your files are successfully uploaded to Dropbox.
}
});
}
}).catch(function (error) {
var showmsg = "Error saving file to your Dropbox!";
reset();
alertify.alert(showmsg);
});
};
}
}
}
rawFile.send(null);
}