【问题标题】:titanium:image upload to server issue钛:图像上传到服务器问题
【发布时间】:2012-03-02 05:07:27
【问题描述】:

朋友们好,

我正在 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.');
  }
}

【问题讨论】:

    标签: titanium-mobile


    【解决方案1】:

    不确定这是否正是您正在寻找的,但我已经成功使用此代码:

    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"]);
    

    【讨论】:

    • 感谢您的帮助,但它对我不起作用,我使用的是 .net webservice
    • @rogerlsmith 我想将媒体作为嵌套参数发送,比如用户{图像:媒体} ....任何线索。我的问题在这里stackoverflow.com/questions/10366838/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 2022-09-26
    • 2021-12-10
    • 2015-03-14
    相关资源
    最近更新 更多