0

I am fetching a list of files (which may be of any type) from mysql database and converting them to byte[] and returning all the files of a particular user as an array of files in spring boot.

Now I need to display all the files in the front end and provide an option to edit the file so that the user can remove the already existing file with the new file.

So Whenever a particular file is edited I have to upload the file from front end using angular js.

The Problem is I am getting the files as an array in response.data. I am facing a problem here like, How to edit the a particular file in that array and send the list of files to the backend to store them.I tried to traverse through array of files and store each file as a blob object, but I am not sure of what is the content type of each file as it can be any type of file(.bmp,.pdf,.docx,.tif etc).

Also I am not sure if it is the rightway to do for editing the files, because it may alter the content of the existing file or may change the type of the exixsting file.

So Please suggest me a way to get the file from the database and edit the file if necessary and then push it back to the database.

My controller and services below:-

angular.module("app").service("serviceCalls", function($q, $rootScope, $http) {
 this.landOwnerEditProfile = function(email){
        var differed = $q.defer();
        $rootScope.config.headers.Authorization = sessionStorage.Authorization;
        $http
            .get($rootScope.URL + "/landownerDetails/"+email+"/", $rootScope.config,{
                responseType: 'blob'
            })
            .then(function(response) {
                console.log("coupon service success");


                differed.resolve(response);
            })
            .catch(function(response) {
                differed.reject(response);
            });
        return differed.promise;
    };

    this.updateLandOwnerProfile = function(details, email) {
            var differed = $q.defer();
            $rootScope.config.headers.Authorization = sessionStorage.Authorization;


            var formdata = new FormData();
            // console.log($("#file"));
            formdata.append("email", email);
            formdata.append("fname", details.user.fname);
            formdata.append("lname", details.user.lname);
            formdata.append("city", details.user.city);
            formdata.append("phone1", details.user.phone1);
            formdata.append("phone2", details.user.phone2);
            formdata.append("phone3", details.user.phone3);
            formdata.append("streetAddressLine1", details.user.streetAddressLine1);
            formdata.append("streetAddressLine2", details.user.streetAddressLine2);
            formdata.append("stripeApiKey", details.user.stripeApiKey);
            formdata.append("zipcode", details.user.zipcode);
            formdata.append("businessName", details.user.businessName);
            formdata.append("landOwnerEditProfile", true);



            // var input_files = document.getElementsByClassName("files");
            // for (var i = 0; i < input_files.length; i++) {
            //     console.log(input_files[i].files);
            //     for (var file of input_files[i].files) formdata.append("file", file.name);
            // }
            // formdata.append("file", input_files);
            // for (var key of formdata.entries()) {
            //     console.log(key[0] + ", " + key[1]);
            // }
            for (var i = 0; i < details.files.length; i++) {    
                formdata.append("file", details.files[i].file);
            }

            $http
                // .post("http://localhost:8090/", formdata, {
                .post($rootScope.URL + "/user/landownerDetails/editProfile/"+email+"/", formdata, $rootScope.config,{
                    transformRequest: angular.identity,
                    headers: {
                        "Content-Type": undefined,
                        // "Authorization":$rootScope.config.headers.Authorization
                    }
                })
                .then(function(response) {
                    // console.log(response);
                    if(response.data.status!=true){
                        throw new Error(" Details Not Updated ");
                    }
                    console.log("Details updated  success");

                    differed.resolve(response);
                })
                .catch(function(response) {
                    console.log("rejected", response);
                    differed.reject(response);
                });
            return differed.promise;

    }})
angular
    .module("app")
    .controller("landOwnerEditProfile", function($state, $scope, $location, serviceCalls, $filter, $stateParams,$timeout) {
        $scope.files = [];
serviceCalls.landOwnerEditProfile(sessionStorage.emailId)
        .then((response)=>{
            console.log(response.data);
            let status = response.status;
                if(status===200){
                    let landownerDetails = response.data;
                    let landowner = landownerDetails.dayleasingusers;
                    let userdocuments =  landownerDetails.userdocuments;// userdocument is an array containing the files
                    $scope.user = {};
                    $scope.property={};
                    $scope.user.fname = landowner.fname;
                    $scope.user.lname = landowner.lname;
                    $scope.user.streetAddressLine1 = landowner.address1;
                    // $scope.user.streetAddressLine2 = landowner.address2 ||'sdasd';
                    $scope.property.propertyType = landowner.state || 'Alabama';
                    $scope.user.city = landowner.city;
                    $scope.user.zipcode = parseInt(landowner.zipCode);
                    $scope.user.phone1 = !!landowner.phone?parseInt(landowner.phone.substring(0,3)):null;
                    $scope.user.phone2 = !!landowner.phone?parseInt(landowner.phone.substring(3,6)):null;
                    $scope.user.phone3 = !!landowner.phone?parseInt(landowner.phone.substring(6)):null;
                    $scope.user.stripeApiKey = landowner.stripeApiKey;
                    $scope.user.businessName = landowner.businessName;
                    $scope.files = [];
                    for(let doc of userdocuments){
                        let blob = new Uint8Array(doc.filedata);

                        $scope.files.push({file:new Blob([blob],{ type:'contentType'})}); //I am not sure of the contentType here.
                        // $scope.files.push({file:new Blob([doc.filedata],{ type:'multipart/form-data'},doc.filename)});
                        // $scope.files.push({file:new File([doc.filedata],doc.filename)});
                    }

                }
                else{
                    throw new Error(response.statusText);
                }

        })
        .catch((error)=>{
            console.log(error);

        })

serviceCalls.updateLandOwnerProfile($scope, sessionStorage.emailId)
                    .then(function(response) {
                        console.log(response);
                    })  
                    .catch(function(error) {
                })
georgeawg
  • 48,608
  • 13
  • 72
  • 95
Sm Srikanth
  • 1,992
  • 1
  • 10
  • 10
  • Avoid using deferred anti-patterns. See [Is this a “Deferred Antipattern”?](https://stackoverflow.com/questions/30750207/is-this-a-deferred-antipattern). – georgeawg May 29 '18 at 11:45
  • In `landOwnerEditProfile`, the `$http.get` is malformed. It only takes two arguments, `url` and `config`. The third argument `{responseType: blob}` is ignored. Is your intention to download a Blob? – georgeawg May 29 '18 at 12:06
  • My intention is to show the file that is in the database, and later that file can be removed and can be replaced by a whole new file.suppose if there are 4 different types of files then user can edit any of the 4 files and have to update it in the database when he clicks on some button. – Sm Srikanth May 29 '18 at 13:30
  • Are you downloading Blob or JSON? If you download a Blob it will have a file type, [`Blob.type`](https://developer.mozilla.org/en-US/docs/Web/API/Blob/type). For more information, see [MDN Web Api Reference - Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob). – georgeawg May 29 '18 at 14:48

0 Answers0