Skip to content

Instantly share code, notes, and snippets.

@1Marc
Created June 23, 2011 00:09
Show Gist options
  • Save 1Marc/1041607 to your computer and use it in GitHub Desktop.
Save 1Marc/1041607 to your computer and use it in GitHub Desktop.
Trying to Upload a file to Dropbox through Titanium Desktop
// relevant dropbox threads:
// - http://forums.dropbox.com/topic.php?id=28728&replies=12#post-
// - http://forums.dropbox.com/topic.php?id=31832
// code depends on spazcore.titanium.js (found in Tweetanium)
// SIMPLE WORKING EXAMPLE
var oa = new SpazOAuth("https://api.dropbox.com/", {"consumerKey": DROPBOX_KEY, "consumerSecret": DROPBOX_SECRET});
oa.setAccessToken(ACCESS_TOKEN, ACCESS_SECRET);
var api_url = "https://api.dropbox.com/0/metadata/dropbox/?list=true",
method = "GET",
xhr = Titanium.Network.createHTTPClient();
xhr.addEventListener(Titanium.HTTP_DONE, function(){
console.log("TI DONE!");
console.log(this.responseText);
});
xhr.setRequestHeader('Authorization', oa.signRequest(method, api_url));
xhr.open(method, api_url);
xhr.send();
// UPLOADING TO DROPBOX (works for text files, not for images)
<!DOCTYPE html>
<html>
<head>
<title>Upload Test</title>
<script src="js/jquery.js"></script>
<script src="js/spazcore.titanium.js"></script>
</head>
<body>
<script>
var oa = new SpazOAuth("https://api.dropbox.com/", {"consumerKey": DROPBOX_KEY, "consumerSecret": DROPBOX_SECRET});
oa.setAccessToken(ACCESS_TOKEN, ACCESS_SECRET);
var dir = Titanium.Filesystem.getResourcesDirectory();
sep = Titanium.Filesystem.getSeparator(),
// filename = 'test.txt',
filename = 'image.png',
filepath = dir + sep + filename;
console.log("FILE");
console.log(filepath);
var uploadStream = Titanium.Filesystem.getFileStream(filepath);
uploadStream.open(Titanium.Filesystem.MODE_READ);
content = uploadStream.read();
uploadStream.close();
console.log("CONTENT");
console.log(content);
var api_url = "https://api-content.dropbox.com/0/files/dropbox/JB?file=" + escape(filename);
var boundary = '----12345568790';
var header = "--" + boundary + "\r\n";
header += "Content-Disposition: form-data; name=\"file\";";
header += "filename=\"" + filename + "\"\r\n\"";
header += "Content-Type: application/octet-stream\r\n\r\n";
var foot = "\r\n--" + boundary + "--";
var fullContent = header + content + foot;
console.log('MESSAGE');
console.log(fullContent);
var xhr = Titanium.Network.createHTTPClient();
xhr.addEventListener(Titanium.HTTP_DONE, function(){
alert('done');
console.log("TI XHR DONE!");
console.log(this.responseText);
});
xhr.open('POST', api_url);
xhr.setRequestHeader('Authorization', oa.signRequest("POST", "https://api-content.dropbox.com/0/files/dropbox/JB", {file: escape(filename)}));
xhr.setRequestHeader("Content-type", "multipart/form-data; boundary=\"" + boundary + "\"");
xhr.setRequestHeader("Connection", "close");
xhr.send(fullContent);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment