如何使用XHR将附件(图像)和嵌套参数发送到titanium的uplaoad文件?

我正在尝试将图片从手机的照片库上传到服务器。

图库正在打开。 这是我的代码。

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之类的东西

  1. 试试ONE

    var params =“image [title] =’helloo helllo’; 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发送而不是附件

  1. 试试两个

    var params =“image [title] =’helloo helllo’; 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=#>, } 

如果我从附件中移除来自图像{}的 图像 ,则它以所需的方式返回对象,即

  "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=#> 

现在真正困惑如何解决这个问题。 谢谢

不确定你是否能够解决这个问题,但我一直在努力解决这个问题,并且能够通过使用以下格式动态生成哈希来获得嵌套参数文件上传工作:

 var params = {}; params['user[user_id]'] = 1; params['user[image][attachment]'] = image; params['user[image][title]'] = "helloo helllo"; params['user[image][description]'] = "Sample Description"; params['user[image][download_type]'] = "free"; params['user[image][price]'] = "0.0"; params['user[image][tag_list]'] = "jddhd"; xhr.send(params); 

如果我尝试使用您在上面提供的格式创建哈希,则图像对象始终作为TiBlob字符串传输。 上面的代码对我有用。