Upload picture from Camera


#1

Hi guys, I am trying to upload a picture to a REST api after I take it from the Camera, but seems like I am not doing it correctly. I hate posted my code so you can help me out where the problem is.

if (!camera.isAvailable()) {return;}
        camera.requestPermissions()
        .then(() => {
            let options = { 
                keepAspectRatio: true, 
                saveToGallery: true 
            };
            camera.takePicture(options)
            .then((imageAsset) => {
                dialogs.prompt({
                    title: Globals.APP_NAME,
                    message: "Picture description (optional)",
                    okButtonText: "OK",
                    cancelButtonText: "Cancel",
                    inputType: dialogs.inputType.text
                }).then(r => {
                    if (r.result) {
                        parameters.Params['Description'] = "";
                        if (r.text != '') parameters.Params['Description'] = r.text;
                    }
                    let source = new imageSourceModule.ImageSource();
                    source.fromAsset(imageAsset).then((source) => {
                        let savepath = fileSystem.knownFolders.documents().path;
                        let filename = 'img_' + new Date().getTime() + '.' + "jpg";
                        let filepath = fileSystem.path.join(savepath, filename);
                        
                        let picsaved = source.saveToFile(filepath, "jpg");
                        if (picsaved) {
                            let session = bghttp.session("image-upload");
                            let request = {
                                url: url,
                                method: "POST",
                                headers: {
                                    "Content-Type": "application/octet-stream",
                                    "File-Name": filename
                                },
                                description: "{ 'uploading': '" + filename + "' }"
                            };
                            let task = session.uploadFile(filepath, request);

                            task.on("progress", this._onUploadProgress);
                            task.on("error", this._onUploadError);
                            task.on("complete", this._onUploadCompleted);
                        }
                        
                    })
                });
            })
            .catch((err) => {
                
            });
        });

Thanks in advance!