我在尝试将文件上传到Titanium appcelerator中的服务器时遇到了很多问题。一切似乎工作正常,但在服务器中它表明发生了错误。这是我的Titanium代码:
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!";
}
我做错了什么?任何帮助都非常感谢。
提前谢谢!
答案 0 :(得分: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网络服务器的人。
谢谢!
答案 1 :(得分:1)
onsendstream。
答案 2 :(得分:0)
使用image.toBlob()
它可以帮助您。
感谢