2012-02-28 79 views
1

朋友你好,钛:图片上传到服务器问题

我开发的应用程序在钛以及使用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.'); 
    } 
} 

回答

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

感谢您的帮助,但它不是为我工作,我所使用的.NET Web服务 – 2012-03-03 05:07:57

+0

@rogerlsmith我要发送的媒体一个嵌套参数说用户{image:media} ....任何线索。我的问题是在这里http://stackoverflow.com/questions/10366838/how-to-send-attachments-images-and-nested-params-using-xhr-to-uplaoad-file-in – 2012-07-27 20:06:59