我正在尝试将图像从手机的照片库上传到服务器。
图片库打开得很好。这是我的代码。
var win = Ti.UI.createWindow({
navBarHidden : true,
});
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();
var main_url = "http://10.0.0.4:3000";
Titanium.Media.openPhotoGallery({
success : function(event) {
Ti.API.info("success! event: " + JSON.stringify(event));
var imageview = 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', main_url + '/images.json');
xhr.setRequestHeader("Connection", "close");
// send the data
var params = "image[attachment]=" + imassage;
xhr.send({
media : imageview,
title : "helloo helllo",
desciption : "Sample Desciption",
username : 'lorem',
password : 'ipsum',
});
},
cancel : function() {
},
error : function(error) {
},
allowImageEditing : true
});
但我想发送嵌套参数,如:
image[media] = image
image[title] = "helloo helllo",
image[desciption] = "helloo helllo",
user[name] = "lorem",
user[password] = "ipsum",
我试过doisg之类的
试一试
var params = "image[title] = 'hello hello'; params = params +"&image[media] = '+ imageview;
进而
等等...
xhr.open('POST', main_url + '/images.json',true);
xhr.setRequestHeader("Connection", "close");
// send the data
xhr.send({
media : imageview,
title : "helloo helllo",
desciption : "Sample Desciption",
username : 'lorem',
password : 'ipsum',
});
但它将图像作为 blob 而不是附件发送
尝试两个
var params = "image[title] = 'hello hello'; params = params +"&image[media] = '+ imageview;
进而
等等...
xhr.open('POST', main_url + '/images.json');
xhr.setRequestHeader("Connection", "close");
// send the data
xhr.send({
media : imageview,
title : "helloo helllo",
desciption : "Sample Desciption",
username : 'lorem',
password : 'ipsum',
});
但它将图像作为 blob 而不是附件发送
- - - - - 编辑 - - - - -
我成功地通过以下方式制作嵌套参数:
xhr.send({
user_id : "1",
image : {
attachment : immage,
'title' : "helloo helllo",
desciption : "Sample Desciption",
download_type : 'free',
price : '0.0',
tag_list : 'jddhd'
},
});
但这返回为:
"image"=>"{
\"title\":\"helloo helllo\",
\"username\":\"lorem\",
\"desciption\":\"Sample Desciption\",
\"order\":\"name\",
\"media\":\"[object TiBlob]\",
\"password\":\"ipsum\"
}
但我需要接收参数,例如:
"image"=>{
"title"=>"hello testing my uploads lorem",
"description"=>"ssasd assdas asdas sad sadsa dsa ",
"download_type"=>"free",
"price"=>"0.0",
"tag_list"=>"jddhd,akhdsa,"
"attachment"=>#<ActionDispatch::Http::UploadedFile:0xb4c713e8 @original_filename="im.jpg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"image[attachment]\"; filename=\"im.jpg\"\r\nContent-Type: image/jpeg\r\n",
@tempfile=#<File:/tmp/RackMultipart20120429-6839-1w8vlxn>>,
}
如果我从附件中删除:image from image{}然后它以所需的方式返回对象,即
"attachment"=>#<ActionDispatch::Http::UploadedFile:0xb4c713e8 @original_filename="im.jpg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"image[attachment]\"; filename=\"im.jpg\"\r\nContent-Type: image/jpeg\r\n",
@tempfile=#<File:/tmp/RackMultipart20120429-6839-1w8vlxn>>
现在真的很困惑如何解决这个问题。谢谢