我正在尝试将一些文件上传到我的网站。我使用了其他人的代码;
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.";
}
?>
有什么不对,还是我需要改变什么?
谢谢!
答案 0 :(得分:1)
这看起来像一个可行的解决方案: https://gist.github.com/furi2/1378595
但是我个人总是将所有二进制内容作为基础,并将其发布到后端以进行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.";
}
?>