简体   繁体   中英

how to connect base64 image and send it using multipart?

Hi I am using NgimgCrop and i successfully got the base64 image Now I want to send it using multipart form data in http post request.

My file upload code look something look like this

scope.myCroppedImage = '';
    $scope.uploadFile = function (file) {
        if (file) {
            // ng-img-crop
            var imageReader = new FileReader();
            imageReader.onload = function (image) {
                $scope.$apply(function ($scope) {
                    $scope.myImage = image.target.result;
                    $scope.profileData.data = $scope.myImage;
                    console.log($scope.profileData.data);
                });
            };
            imageReader.readAsDataURL(file);
        }
    };

Is there any way it convert it multipart after base64 in angular ? I tried various links but nothing worked.

You need to send it with FormData:

YourAppName.config(function($httpProvider) {

    $httpProvider.defaults.transformRequest = function(data) {

    var fd = new FormData();
    angular.forEach(data, function(key, value) {
        fd.append(key, value);
    });

    return fd;

    }

});

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