0

私が取り組んでいるアプリケーションは、別のチームが開発した API を使用しています。私はTitanium 2.1.2に取り組んでおり、上記のAPIを使用して写真をアップロードしようとしています. Appcelerator の HTTPClient を使用してリクエストを作成しています。これが私のコードです:

    var url = 'http://api.veramiko.com/albums/' + album.veramiko_id + '/photos';
    var photo = imageView.toBlob();

    var args = { //parameters sent to post photo
        file : photo,
        description : descriptionText
    };

    var client = Ti.Network.createHTTPClient({
        onload : function(e){
            Ti.API.info(this.responseText); //Print the result
        },
        onerror : function(e){
            Ti.API.error(this.responseText); //Print the result
        },
        timeout : 60000
    });
    client.open('POST', url);
    client.setRequestHeader('Authorization', 'Bearer ' + token);
    client.setRequestHeader('Content-type', "multipart/form-data");
    client.send(args);

トークンは、サーバーへのリクエストを承認するために使用する変数です。ImageView の画像を Blob に変換するだけで写真を送ることができると思ったのですが、写真がアップロードされません。投稿は説明付きで作成されますが、写真が正しく送信されません。

他に何か追加する必要がありますか? 写真を Blob として送信するのは正しいですか?

編集:私はこのリンクを読んで、結果なしで次のことを試しました:

    var url = 'http://api.veramiko.com/albums/' + album.veramiko_id + '/photos';
    var boundary = '-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var photo = imageView.toBlob();

    var args = {
        file : photo,
        description : descriptionText.value
    };      

    var contentDisposition = "--" + boundary + "\r\n"; 
    contentDisposition += "Content-Disposition: form-data; name=\"file\";";
    contentDisposition += "filename=\"" + imageView.image + "\"\r\n\";";
    contentDisposition += "Content-Type: application/octet-stream\r\n\r\n";

    var fullContent = contentDisposition + photo  + "\r\n--" + boundary + "--";

    alert(JSON.stringify(args));
    var token = JSON.parse(Ti.App.Properties.getString('loggedUser', 'No existe')).networks[0].token;
    var client = Ti.Network.createHTTPClient({
        onload : function(e){
            Ti.API.info(this.responseText); //Print the result
        },
        onerror : function(e){
            Ti.API.error(this.responseText); //Print the result
        },
        timeout : 60000
    });
    client.open('POST', url);
    client.setRequestHeader('Authorization', 'Bearer ' + token);
    client.setRequestHeader('Content-Type', "multipart/form-data; boundary=\"" + boundary + "\"");
    client.setRequestHeader('Connection', 'close');
    client.send(fullContent);

Content-Disposition および Content-Type ヘッダーでファイルをラップしようとしましたが、結果はありませんでした。

4

4 に答える 4

1

私は最終的にこれを解決する方法を見つけました。以下のリンクを参照してください。

これは私のコードが最終的にどのように見えるかです:

    // I put the contents of my image into a variable
var f = imageHolder.toImage();
    // I create a random name for the image
var tmpName = (new Date()).getTime().toString() + '.png';
    // I create a folder where I will store the photo temporally
var g = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'picturesToUpload');
if (!g.exists()) {
    // If the directory doesn't exist, make it
    g.createDirectory();
};
var photo = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'picturesToUpload', tmpName);
    // write the contents of the image into the file
photo.write(f);
    // I create my url with the albumId where I'll upload the picture
var url = 'http://api.veramiko.com/albums/' + albumId + '/photos';
var args = {
    file : photo.read(), // I read the contents of the file
    description : ''
}
var token = JSON.parse(Ti.App.Properties.getString('loggedUser', 'No existe')).networks[0].token;
var client = Ti.Network.createHTTPClient({
    onload : function(e){
        Ti.API.info('Info received from the uploading: ' + this.responseText);
    },
    onerror : function(e){
        Ti.API.debug('Error: ' + this.responseText);
    },
        timeout : 60000
});
client.open('POST', url);
    // these headers are important
client.setRequestHeader('enctype', 'multipart/form-data');
client.setRequestHeader('Content-Type', 'image/png');
client.setRequestHeader('Authorization', 'Bearer ' + token);
client.send(args);

この情報がより多くの人に役立つことを願っています。

于 2012-10-05T19:09:06.260 に答える
0

まず、APIパラメータをクリアします。TOKENを使いたい場所。

var imageView = Ti.UI.createImageView({
    backgroundImage : "image.png",
});
    var url = 'http://api.veramiko.com/albums/' + album.veramiko_id + '/photos';


        var args = { //parameters sent to post photo
            file : imageView.backgroundImage,
            description : descriptionText,
            token : "Token",
        };

        var client = Ti.Network.createHTTPClient({
            onload : function(e){
                Ti.API.info(this.responseText); //Print the result
            },
            onerror : function(e){
                Ti.API.error(this.responseText); //Print the result
            },
            timeout : 60000
        });
        client.open('POST', url);
        client.setRequestHeader("Content-type","multipart/form-data");
        client.setRequestHeader("Content-length", args.length);
        client.send(args);

これを試してください、私はこれがあなたのために働いていると思いました...

于 2012-09-05T05:21:20.473 に答える
0

アップローダの作成方法を説明するこのチュートリアルを見つけました。

Titanium Mobile: 画像アップローダーを構築する

于 2012-09-25T16:25:00.133 に答える
0

このコードを使用して、ファイルを form-data にアップロードします。

var baseurlAPI = "YOUR API BASEURL HERE";
var file = Ti.Filesystem.getFile(pathToFile);

 if(file.exists()){
    var xhr = Ti.Network.createHTTPClient({
        onload: function(e) {
                Ti.API.log('success '+this.responseText);
       },
       onerror: function(e) {
                Ti.API.error(this.responseText);
        },
            timeout : -1
    });
    xhr.open('POST', baseurlAPI);
    xhr.send({
       file: file
    });
 }else{
    console.log('didnt exist ' + file.nativePath);
 }
于 2020-02-05T05:44:49.173 に答える