2

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

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

    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. 试一试

    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 而不是附件发送

  1. 尝试两个

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

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

4

1 回答 1

1

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

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

于 2012-11-30T18:54:36.253 回答