2013-12-13 171 views
0

我在嘗試上傳文件到Titanium appcelerator中的服務器時遇到了很多問題。似乎所有工作都很好,但在服務器中顯示發生了錯誤。這裏是我的鈦代碼:Titanium Appcelerator將圖像上傳到網絡服務器

var win = Ti.UI.createWindow({ 

backgroundColor : "#FFF" 
}); 


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(); 

    win.open(); 

     Titanium.Media.openPhotoGallery({ 

success : function(event) { 
    alert("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://mypathtotheserver/myphpuploaderfile.php'); 
    xhr.setRequestHeader("Connection", "close"); 
    // send the data 
    xhr.send({ 
     media : image 
    }); 

}, 
cancel : function() { 

}, 
error : function(error) { 
}, 
allowImageEditing : true 

});

,並在服務器上的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!"; 
} 

我在做什麼錯?任何幫助,高度讚賞。

預先感謝您!

回答

1

好了花了很長時間試圖讓這個工作,我終於找到了正確的答案。我會在這裏分享給那些遇到問題的人看看並修復照片上傳問題。我還沒有測試過使用Android,但它應該都是一樣的。

這裏的鈦 'app.js' 代碼:

var win = Ti.UI.createWindow({ 

     backgroundColor : "#FFF" 
    }); 

    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(); 

    win.open(); 

    //Not a necessary function, but just in case you want to randomly create names 
    // for each photo to be uploaded 
    function randomString(length, current) { 
     current = current ? current : ''; 
     return length ? randomString(--length, "abcdefghiklmnopqrstuvwxyz".charAt(Math.floor(Math.random() * 60)) + current) : current; 
    } 

Titanium.Media.openPhotoGallery({ 

success : function(event) { 

    var image = event.media; 

    var xhr = Titanium.Network.createHTTPClient(); 

    xhr.open('POST', 'http://yoursite.com/scriptsfolder/upload.php'); 

    xhr.onerror = function(e) { 
     Ti.API.info('IN ERROR ' + e.error); 
    }; 
    xhr.onload = function(response) { 

     if (this.responseText !=0){ 
      var imageURL = this.responseText; 
      alert('Your image was uploaded to ' +imageURL); 
     }else { 

      alert("The upload did not work! Check your PHP server settings."); 
     } 

    }; 
    xhr.onsendstream = function(e) { 
     ind.value = e.progress; 

    }; 

     // send the data 
     var r = randomString(5) + '.jpg'; 
     xhr.send({ 
     'media': image, 
     'randomFilename' : r 
     }); 

    }, 
    cancel : function() { 

    }, 
    error : function(error) { 
    }, 
    allowImageEditing : true 
    }); 

這裏是PHP服務器端腳本。您將需要上傳這個PHP文件到您的網絡服務器:

<?php 

     $target = getcwd(); 
     $target = $target .'/'. $_POST['randomFilename']; 


     if (move_uploaded_file($_FILES['media']['tmp_name'], $target)) { 

     $filename = $target; 

    //Get dimensions of the original image 

     list($current_width, $current_height) = getimagesize($filename); 

    // The x and y coordinates on the original image where we will begin cropping the image 
    $left = 0; 
    $top = 0; 

    //This will be the final size of the image (e.g how many pixesl left and down we will be doing) 
    $crop_width = $current_width; 
    $crop_height = $current_height; 

    //Resample the image 
    $canvas = imagecreatetruecolor($crop_width, $crop_height); 
    $current_image = imagecreatefromjpeg($filename); 
    imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height); 
    imagejpeg($canvas, $filename, 100); 

    echo 'http://yoursite.com/scriptsfolder/'.$_POST['randomFilename']; 

     }else { 

     echo "0"; 
     } 

    ?> 

那裏你有它。我不得不說,我從boydlee的appcelerator食譜找到了這個問題的答案。

我希望這可以幫助那些努力將照片上傳到Titanium自己的網絡服務器的人。

謝謝!

+0

您好在iOS上工作,但不能在android上工作? wat做什麼?任何暗示 – joe

0

使用image.toBlob()它可以幫助你。 謝謝

+0

嗨trippino。感謝您花時間閱讀並回答我的問題。對我來說,toBlob函數似乎只能在圖像附加到imageView時才能調用,而在這種情況下並不是這樣。另外,根據我的理解(我可能是錯的),從Ti.Media返回的圖像已經變成了一個blob。我錯了嗎?謝謝 – ito

1

onsendstream必須在open之前設置。

相關問題