Titanium试图将文件上传到服务器

I'm trying to upload some files to my website. I used this code from someone else;

var xhr = Titanium.Network.createHTTPClient();


var file = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory + 'text.txt');
Titanium.API.info(file);
var toUpload = file.read();

xhr.open('POST', 'http://www.domain.com/upload.php', false);
xhr.send({media: toUpload});

I tried to run my app with this method, it says it's done uploading, but when I look, my files are not there.

Also I used this PHP file to handle the upload;

<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
?>

Is there anything wrong, or do I have to change something?

Thanks!

This looks like a viable solution: https://gist.github.com/furi2/1378595

However I personally always base64 all binary content and just post it to the backend to be base64 decoded.

On the Titanium side:

var xhr = Titanium.Network.createHTTPClient();

var file = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory + 'text.txt');
Titanium.API.info(file);
var toUpload = Titanium.Utils.base64encode(file)

xhr.open('POST', 'http://www.domain.com/upload.php', false);
xhr.send({media: toUpload, media_name: 'text.txt'});

And on the PHP side:

<?php
$target = "upload/";
$target = $target . $_POST['media_name'];
$data = base64_decode($_POST['media']);
$ok=1;
if(file_put_contents($target, $data))
{
echo "The file ". ( $_POST['media_name']). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
?>