doushadu0901 2013-07-06 16:54
浏览 16

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!

  • 写回答

1条回答 默认 最新

  • duan1933 2013-07-12 12:18
    关注

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

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计