0

我正在从 mysql 数据库中获取文件列表(可能是任何类型)并将它们转换为 byte[] 并在 spring boot 中将特定用户的所有文件作为文件数组返回。

现在我需要在前端显示所有文件并提供编辑文件的选项,以便用户可以使用新文件删除已经存在的文件。

因此,每当编辑特定文件时,我都必须使用 Angular js 从前端上传文件。

问题是我在 response.data 中将文件作为数组获取。我在这里遇到一个问题,例如如何编辑该数组中的特定文件并将文件列表发送到后端以存储它们。我试图遍历文件数组并将每个文件存储为 blob 对象,但我我不确定每个文件的内容类型是什么,因为它可以是任何类型的文件(.bmp、.pdf、.docx、.tif 等)。

另外我不确定这是否是编辑文件的正确方法,因为它可能会改变现有文件的内容或可能会改变现有文件的类型。

所以请建议我一种从数据库中获取文件并在必要时编辑文件然后将其推回数据库的方法。

我的控制器和服务如下:-

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) {
                })
4

0 回答 0