簡體   English   中英

Titanium嘗試將文件上傳到服務器

[英]Titanium trying to upload file to server

我正在嘗試將一些文件上傳到我的網站。 我使用了別人的這段代碼;

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

有什么問題嗎,還是我必須改變一些東西?

謝謝!

這看起來是一個可行的解決方案: 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.";
}
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM