2010-03-28 83 views
3

我終於開始使用Titanium Mobile開發iPhone應用程序。現在我面對的問題是,我能夠運行該應用程序,並且該應用程序還將該映像發送到服務器。但我無法看到上傳到服務器的文件。我粘貼了iPhone應用程序的代碼以將圖像發送到服務器,並且還將從應用程序接收文件的PHP文件。如何從使用鈦開發的iPhone應用程序上傳圖像

var win = Titanium.UI.currentWindow; 

var ind=Titanium.UI.createProgressBar({ 
width:200, 
height:50, 
min:0, 
max:1, 
value:0, 
style:Titanium.UI.iPhone.ProgressBarStyle.PLAIN, 
top:10, 
message:'Uploading Image', 
font:{fontSize:12, fontWeight:'bold'}, 
color:'#888' 
}); 

win.add(ind); 
ind.show(); 

Titanium.Media.openPhotoGallery({ 

success:function(event) 
{ 
    Ti.API.info("success! event: " + JSON.stringify(event)); 
    var image = event.media; 

    var xhr = Titanium.Network.createHTTPClient(); 

    xhr.onerror = function(e) 
    { 
     Ti.API.info('IN ERROR ' + e.error); 
    }; 
    xhr.onload = function() 
    { 
     Ti.API.info('IN ONLOAD ' + this.status + ' readyState ' + this.readyState); 
    }; 
    xhr.onsendstream = function(e) 
    { 
     ind.value = e.progress ; 
     Ti.API.info('ONSENDSTREAM - PROGRESS: ' + e.progress+' '+this.status+' '+this.readyState); 
    }; 
    // open the client 
    xhr.open('POST','http://www.myserver.com/tmp/upload2.php'); 
    xhr.setRequestHeader("Connection", "close"); 
    // send the data 
    xhr.send({media:image}); 

}, 
cancel:function() 
{ 

}, 
error:function(error) 
{ 
}, 
allowImageEditing:true 
}); 

這裏是服務器上的PHP代碼:http://www.pastie.org/891050

我不知道我要去哪裏錯了。請幫我解決這個問題。如果您需要更多信息,願意提供。

回答

2

使用下面的PHP代碼:

$target_path = "uploads/"; 

$target_path = $target_path . $_FILES['media']['name']; 

if(move_uploaded_file($_FILES['media']['tmp_name'],$target_path)) { 
echo "The file ". basename($_FILES['media']['name']). 
" has been uploaded"; 
} 
else 
{ 
echo "There was an error uploading the file, please try again!"; 
} 
+0

嗨, 謝謝!現在它工作正常!但我不能得到擴展名的文件!就像iPhone一樣,iPhone上傳的圖片沒有任何獨特的名稱或擴展名! – 2010-04-11 05:17:26

相關問題