2012-02-28 85 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