如何使用XHR发送附件(图像)和嵌套参数,以钛加工uplaoad文件? [英] How to send attachments (images) and nested params using XHR to uplaoad file in titanium?

查看:75
本文介绍了如何使用XHR发送附件(图像)和嵌套参数,以钛加工uplaoad文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图片从手机的相册上传到服务器.

I am trying to upload images from the photo gallery of the phone to the server.

图片库打开得很好.这是我的代码.

Images gallery is opening perfectly fine. Here is my code.

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

但是我想发送嵌套参数,如:

But i want to send nested parameters like :

image[media] = image
image[title] = "helloo helllo",
image[desciption] = "helloo helllo",
user[name] = "lorem",
user[password] = "ipsum",

我尝试过类似的操作

  1. 尝试一个

var params ="image [title] ='helloo helllo'; params = params +& image [media] ='+ imageview;

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',
});

但是它将图像发送为斑点而不是附件

but it sends the image as a blob and not attachment

  1. 尝试两次

var params ="image [title] ='helloo helllo'; params = params +& image [media] ='+ imageview;

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',
});

但是它将图像发送为斑点而不是附件

but it sends the image as a blob and not attachment

----------编辑----------

我成功地通过以下方式制作了嵌套参数:

I succedded in making nested params by :

    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\"
}

但是我需要像这样接收参数:

but i need to have parameters to be recieved like :

"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>>,
}

,如果我从图像{} 附件:图像中删除,则它将以所需的方式(即

and if I remove from attachment : image from image{} then it returns the object in desired way i.e.

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

现在真正的困惑是如何解决此问题.谢谢

Now in real confusion how to resolve this issue. Thanks

推荐答案

不确定您是否能够解决此问题,但是我为此苦苦挣扎了一段时间,并且能够动态地上传嵌套参数文件以使其正常工作使用以下格式生成哈希:

Not sure if you were able to solve this, but I was struggling with this for a little while and was able to get nested parameter file uploading to work by dynamically generating the hash using the following format:

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字符串传输.上面的代码对我有用.

if i tried to create the hash using the format you provided above, the image object was always being transferred as a TiBlob string. The above code is working for me.

这篇关于如何使用XHR发送附件(图像)和嵌套参数,以钛加工uplaoad文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆