简体   繁体   中英

Problems with File plugin in Ionic2

I'm integrating Quickblox in my Ionic2 app, for now I was able to do all the things except uploading a file. In Quickblox you have to upload a file using a function made by them that according to js documentation looks like this:

var inputFile = $("input[type=file]")[0].files[0];

var params = {name: inputFile.name, 
     file: inputFile, 
     type: inputFile.type, 
     size: inputFile.size, 
     public: false};

QB.content.createAndUpload(params, function(err, response){
  if (err) {
    console.log(err);
  } else {
    console.log(response);
    var uploadedFile = response;
    var uploadedFileId = response.id;
  }
});

So I translated above code to typescript and I have something like this:

uploadFile(filename) {
    File.resolveDirectoryUrl(cordova.file.dataDirectory).then(
        (dirEntry) => {
            File.getFile(dirEntry, filename, { create: false }).then(
                (fileEntry) => {
                    console.log(fileEntry);
                    fileEntry.file((file) => {
                        console.log(file);
                        var params = {
                            name: file['name'],
                            file: file,
                            type: file['type'],
                            size: file['size'],
                            'public': false
                        };
                        quickblox.content.createAndUpload(params,
                            (err, response) => {
                                if (err) {
                                    console.log(err);
                                } else {
                                    console.log(response);
                                    var uploadedFileId = response.id;
                                    var msg = {
                                        type: 'groupchat',
                                        body: "[attachment]",
                                        extension: {
                                            save_to_history: 1,
                                        }
                                    };
                                    msg["extension"]["attachments"] = [{ id: uploadedFileId, type: 'photo' }];
                                    quickblox.chat.send(this.xmpp_room_jid, msg);
                                }
                            });
                    })
                }).catch(
                (err) => {
                    console.log(err);
                }
                );
        }
    );
}

This work in the terms of "I get ok responses from quickblox server", but when I go to the admin console of quickblox to check the uploaded content I find out that the image I uploaded has 0 bytes. So after checking the code for a while I compared side by side all my function calls with the example app of quickblox and the only difference I could find was in the File object.

This is the File object I get in my Ionic 2 app:

离子2代码

And this is the one I get in the quickblox js example:

Quickblox js代码

All the others things looks identically except this File object. I'm almost sure that this is the problem I'm having, and because I'm very newbie in this field, I couldn't find a way to cast from my File object in Ionic to something like the File object in the js example. Thanks in advance at all for your time and help.

EDIT:

I attach the requests/responses logs from my Ionic app:

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

Could you please post the code you used to connect to chat, create a session, open a video call? The documentation on quickblox is very bad and i got stuck at connecting to chat.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM