Skip to content

Instantly share code, notes, and snippets.

@jonalter
Created July 11, 2011 19:24
Show Gist options
  • Save jonalter/1076589 to your computer and use it in GitHub Desktop.
Save jonalter/1076589 to your computer and use it in GitHub Desktop.
Titanium Desktop file upload example
<html>
<head></head>
<body style="background-color:#ffffff;margin:10px">
<script>
var uploadFile = function(){
var text = document.getElementById('txt');
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 xhr = Titanium.Network.createHTTPClient();
xhr.addEventListener(Titanium.HTTP_DONE, function(){
//alert('done');
console.log("TI XHR DONE!");
console.log(this.responseText);
text.innerHTML += "\n" + this.responseText;
});
xhr.addEventListener(Titanium.HTTP_STATE_CHANGED, function(){
//alert('changed');
console.log("STATE CHANGED");
console.log(this.responseText);
text.innerHTML += "\n" + this.responseText;
});
xhr.open('POST', "http://localhost/~jalter/uploads.php");
xhr.setRequestHeader("contentType", "multipart/form-data");
xhr.send({uploadedfile:content});
}
</script>
<input type="button" value="Upload" onClick="uploadFile()">
<textarea id="txt" style="width: 100%; height: 100%;"></textarea>
</body>
</html>
<?php
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name'] . ".png");
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name'])." has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
// portions of this code where borrowed from pec1985
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment