如何从使用Titanium开发的iPhone应用程序上传图像 [英] How to upload images from iPhone app developed using Titanium

查看:75
本文介绍了如何从使用Titanium开发的iPhone应用程序上传图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我终于开始使用Titanium Mobile开发iPhone应用程序了。现在我遇到的问题是,我能够运行应用程序,应用程序也将图像发送到服务器。但我无法看到上传到服务器的文件。我已粘贴iPhone应用程序的代码以将图像发送到服务器,以及将从应用程序接收文件的PHP文件。

I finally landed up in developing an iPhone app using Titanium Mobile. Now the problem I face is, Im able to run the app, and the app also sends the image to the server. But Im not able to see the file that got uploaded to the server. I have pasted the iPhone app's code to send image to the server and also, the PHP file that would receive the file from the app.

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

And here is the PHP code on the server: http://www.pastie.org/891050

我不知道我在哪里我出错了。请帮我解决这个问题。如果您需要更多信息,我们愿意提供。

I'm not sure where I'm going wrong. Please help me out in this issue. Would love to provide if you need some more information.

推荐答案

使用以下代码获取Php:

use following code for 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!";
}

这篇关于如何从使用Titanium开发的iPhone应用程序上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆