NativeScript / nativescript-background-http

Background Upload plugin for the NativeScript framework
Apache License 2.0
101 stars 50 forks source link

Im trying to take a picture and upload it with backgroundHttp but it doesnt work #200

Closed tsonevn closed 5 years ago

tsonevn commented 5 years ago

@ozimanik commented on Wed Jan 23 2019

function openCam(args) {
    camera.takePicture()
        .then(imageAsset => {

            var savepath = fs.knownFolders.documents().path;
            var filename = 'img_' + new Date().getTime() + '.jpg';
            var filepath = fs.path.join(savepath, filename);

            var imageSource = new imageSourceModule.ImageSource;

            imageSourceModule.fromAsset(imageAsset).then(res => {
                imageSource = res;
                var picsaved = imageSource.saveToFile(filepath, "jpg");

                console.log("filepath: " + filepath);

                if (picsaved) {

                    var session = bgHttpModule.session("file-upload");

                    var request = {
                        url: appSettings.getString("url") + "/Public/UploadImage" + "?username=" + appSettings.getString("username") + "&passwort=" + appSettings.getString("passwort") + "&kalender=" + appSettings.getString("kalender") + "&evid=2996",
                        method: "POST",
                        headers: {
                            "Content-Type": "application/octet-stream",
                            "File-Name": filename
                        }
                    };

                    var task = session.uploadFile(filepath, request);
                    task.on("progress", logEvent);
                    task.on("error", logEvent);
                    task.on("complete", logEvent);

                } else {
                    console.log("Failed To Save");
                }
            })
        });
}
tsonevn commented 5 years ago

Hi @ozimanik, I tested the described scenario in a sample project, however, was unable to recreate an issue with the plugin. I am attaching the sample project, that I used for testing. You can review it and make the needed changes, which can help us to recreate the problem. Archive.zip