0

我正在尝试将一些文件上传到我的网站。我使用了其他人的代码;

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});

我尝试使用这种方法运行我的应用程序,它说它已完成上传,但是当我查看时,我的文件不存在。

我也使用这个 PHP 文件来处理上传;

<?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.";
}
?>

有什么问题吗,还是我必须改变一些东西?

谢谢!

4

1 回答 1

1

这看起来是一个可行的解决方案: https ://gist.github.com/furi2/1378595

但是,我个人总是对所有二进制内容进行 base64 处理,然后将其发布到后端进行 base64 解码。

在钛方面:

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'});

在 PHP 方面:

<?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.";
}
?>
于 2013-07-12T12:18:33.377 回答