Ionic app image upload from camera / photo library

后端 未结 2 1981
囚心锁ツ
囚心锁ツ 2020-12-13 07:33

I\'m working on a ionic chat app where the user can upload a photo as part of their message. I\'m looking for a way to upload the image to my webhost server so I can retriev

2条回答
  •  半阙折子戏
    2020-12-13 07:57

    the app I am building for a company had the same issue, what we did is we just posted the image to our server as a base64 string. Then you can simple pull the string from the database and display it in a div. We used the NgCordova camera and then just pass in the data from the takePhoto function.

    $scope.takePhoto = function () {
                $ionicScrollDelegate.scrollTop();
                console.log('fired camera');
                $scope.uploadList = false;
                $ionicPlatform.ready(function() {
                    var options = {
                        quality: 100,
                        destinationType: Camera.DestinationType.DATA_URL,
                        sourceType: Camera.PictureSourceType.CAMERA,
                        allowEdit: false,
                        encodingType: Camera.EncodingType.PNG,
                        targetWidth: 800,
                        targetHeight: 1100,
                        popoverOptions: CameraPopoverOptions,
                        saveToPhotoAlbum: false
                    };
                    $cordovaCamera.getPicture(options).then(function (imageData) {
                        $ionicLoading.show({
                            template: 'Processing Image',
                            duration: 2000
                        });
                        $scope.image = "data:image/png;base64," + imageData;
                        if (ionic.Platform.isAndroid() === true) {
                            $scope.Data.Image = LZString.compressToUTF16($scope.image);
                            $scope.Data.isCompressed = 1;
                        } else {
                            $scope.Data.Image = $scope.image;
                            $scope.Data.isCompressed = 0;
                        }
                        if ($scope.tutorial) {
                            $scope.showAlert("Instructions: Step 3", '
    Now that you have taken a photo of the POD form, you must upload it to the server. Press the upload doc button in the bottom right of the screen.
    '); } $scope.on('') }, function (err) { console.log(err); }); }, false); }; $scope.UploadDoc = function () { var req = { method: 'POST', url: ffService.baseUrlAuth + 'cc/upload', headers: { 'x-access-token': ffService.token }, data: $scope.Data }; if ($scope.Data.Image === null || $scope.Data.Value === '') { $scope.showAlert("Uh Oh!", '
    Please take a photo of your document before attempting an upload.
    '); } else { $http(req).success(function (data, status, headers, config) { localStorage.setItem('tutorial', false); $scope.tutorial = false; $scope.getUploads($scope.PODOrder.OrderNo); $scope.showAlert("Success!", '
    Your Document has been successfully uploaded!
    '); $scope.uploadList = true; }).error(function (data, status, headers, config) { $rootScope.$broadcast('loading:hide'); $scope.showAlert("Something went wrong!", '
    Please make sure you have an internet connection and try again.
    '); }).then(function(data, status, headers, config){ $scope.Data.Image = null; }); } };

提交回复
热议问题