从Cordova Apps将文件上传到Nodejs。从相机拍摄的图像

时间:2019-08-22 22:44:59

标签: javascript node.js ajax cordova multer

这是我用来在Cordova App中捕获图像的代码

  $scope.cameraCapture = function() {



        navigator.camera.getPicture(function(cameraResponse) {


            warn("Response From Camera :");

            var image = "data:image/jpeg;base64," + cameraResponse;



            window.resolveLocalFileSystemURL(cameraResponse, function success(fileEntry) {

                // Do something with the FileEntry object, like write to it, upload it, etc.
                // writeFile(fileEntry, imgUri);

                // displayFileData(fileEntry.nativeURL, "Native URL");

                fileEntry.file(function(file) {

                    restServices.pictureGenderValidator(file, function(isImageUploaded) {
                        console.log(isImageUploaded);

                    });

                });


            }, function() {
                // If don't get the FileEntry (which may happen when testing
                // on some emulators), copy to a new FileEntry.
                createNewFileEntry(cameraResponse);
            });

        }, $scope.cameraError, {
            destinationType: Camera.DestinationType.FILE_URI,
        });


    }

这是我用来上传的服务功能。 但是,两者都不起作用

pictureGenderValidator: function(image, cb) {

                    error("FileData/Form Data Upload To BackEnd");
                    log(image);
                    var fileFormData = new FormData();
                    fileFormData.append('file', image);
                    log(fileFormData);
                    $http({
                        method: 'POST',
                        url: restConfig.pictureGenderValidator,
                        transformRequest: angular.identity,
                        headers: {
                            'Content-Type': 'multipart/form-data',
                            "X-File-Name": image.name,
                            'X-File-Size': image.size,
                            'X-File-Type': image.type
                        },
                        params: {
                            image: fileFormData,
                            image2: image
                        }
                    }).then(function(resp) {
                        cb(resp);
                    });
                },
                pictureGenderValidatorBase64: function(base64, cb) {

                    error("FileData/Form Data Upload To BackEnd");
                    log(base64);
                    $http({
                        method: 'POST',
                        url: restConfig.pictureGenderValidatorBase64,
                        transformRequest: angular.identity,
                        headers: { 'Content-Type': undefined },
                        params: { image: base64 }
                    }).then(function(resp) {
                        cb(resp);
                    });
                }

我正在使用NODEJS / multer进行文件上传。使用Uppy File服务时,我可以在NODEJS上发送和接收文件。

但是我无法使用Cordova发送文件,因为我使用FILE_URI作为向我发送文件URI的相机选项。

NODEJS代码:

app.post('/get/picture/validator', mutlerUpload.single("file"), function(req, resp, next) {
        var image = req.files;
        var image2 = req.body.image2 || req.query.image2;

        log('/get/picture/validator');
        log(image);
        log(image2);
        resp.send({ image: image, message: 'Received', image2: image2 });
    });


    app.post('/get/picture/validator/base', function(req, resp) {
        log('/get/picture/validator/base');
        log("This IS a Base 64 Receiver");
        var image = req.body.image || req.query.image || req.param("profile");
        log(image);
        resp.send({ image: image, message: 'Received' });
    });

0 个答案:

没有答案