钛:图像上传到服务器问题

时间:2012-02-28 05:46:56

标签: titanium-mobile

朋友你好,

我正在使用Titanium开发应用程序并开发一个功能使用POST方法将图像上传到服务器,我从照片库中选择一张照片并发送到服务器但是我无法获得从服务器成功响应,我想发送图像名称作为参数,如45645.png和媒体参数等,但我无法发送图像名称所以请告诉我如何解决我的问题。

将上传图片提交给服务器:http://mobile.tutsplus.com/tutorials/appcelerator/titanium-mobile-build-an-image-uploader/

//photo gallery for  select photo 
function getPhotGallery () 
{
  Titanium.Media.openPhotoGallery({

    success:function(event)
    {
        //var cropRect = event.cropRect;
        var image = event.media;

        // set image view
        Ti.API.debug('Our type was: '+event.mediaType);
        if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO)
        {
            uploadPhotoImageView.image = image;

            UploadPhotoToServer(uploadPhotoImageView.image);
        }
        else
        {

        }
        //Titanium.API.info('PHOTO GALLERY SUCCESS cropRect.x ' + cropRect.x + ' cropRect.y ' + cropRect.y  + ' cropRect.height ' + cropRect.height + ' cropRect.width ' + cropRect.width);
    },
    cancel:function()
    {

    },
    error:function(error)
    {
    },
    allowEditing:true,
    //popoverView:popoverView,
    //arrowDirection:arrowDirection,
    mediaTypes:[Ti.Media.MEDIA_TYPE_PHOTO]
    });

}


//upload photo to server uisng POST method
function UploadPhotoToServer(media)
{
        //var filename = mobileNumber.value + '.png';
        var filename = '123456.png';

    if (Titanium.Network.online == true) 
    {  
       var imgUploadLoader = Titanium.Network.createHTTPClient();

            //open the client
          imgUploadLoader.open('POST', 'http://projects.spinxweb.net/ContactsTracking/iphone-file-upload.aspx'); 

         // send the data
         imgUploadLoader.send(
         {
             media: media,
             "name": filename
         });

       imgUploadLoader.onerror = function(e)
       {
            Ti.API.info('IN ERROR ' + e.error);
            alert('Sorry, we could not upload your photo! Please try again.');
       };

       imgUploadLoader.onload = function()
       {
            Ti.API.info('IN ONLOAD ' + this.status + ' readyState ' + this.readyState);
            if(this.responseText != 'false')
            {   
                var url = this.responseText; //set our url variable to the response                 
                Ti.API.log('Upload image url:'+url);    
                //alert('Upload photo successfully');

                //getLoginData();                                       
            }
            else 
            {
                alert('Whoops, something failed in your upload script.');                   
            }           
       };

      imgUploadLoader.onsendstream = function(e)
      {
          Ti.API.info('ONSENDSTREAM - PROGRESS: ' + e.progress);
          if(Ti.Platform.osname == 'android')
          {

          }
          else 
          {

          }
      };


  }     
  else
  {
    alert('You must have a valid Internet connection in order to upload this photo.');
  }
}

1 个答案:

答案 0 :(得分:2)

不确定这是否正是您所需要的,但我已成功使用此代码:

eventSuccess : function ( e )
{
  var xhr = Ti.Network.createHTTPClient ( );
  xhr.open ( "POST", 'yourserver.com/webservice.php' );

  xhr.setTimeout ( 20000 );


  xhr.send ( 
  {         
    "CommandType"    : "image",
    "file"           : e.media,
    "name"           : filename
  });

  xhr.onload = function ( e )
  {
    Ti.API.info ("image sent to server");
  }
}

当从相机返回成功事件时,此操作正在运行。然后将图像发送到服务器。

然后你需要服务器端的东西,比如:

move_uploaded_file ($_FILES["file"]["tmp_name"], "incoming/images/" . $_FILES["file"]["name"]);