文件没有使用FormData绑定到angularjs服务

时间:2018-03-04 07:54:16

标签: angularjs angular-promise angular-services angular-file-upload

我正在尝试使用angularjs和spring 3上传文件,我编写了一个不绑定所选文件的服务。我不知道我一直在做什么错,请帮我解决这个问题。我使用commonsmultipart上传文件。以下是fileUploadService和控制器代码。

MyApp.service('fileUploadService', function ($http, $q) {        
    this.uploadFileToUrl = function (file, uploadUrl, formData) {
        //FormData, object of key/value pair for form fields and values
        var fileFormData = new FormData();
        fileFormData.append('fileUpload', file);
      //  console.log(formData);
        fileFormData.append('name', formData.name);
        fileFormData.append('email', formData.email);
        fileFormData.append('password', formData.password);
        fileFormData.append('mobile_no', formData.mobile_no);
        fileFormData.append('register-submit', 'Register Now');

        var deffered = $q.defer();
        $http.post(uploadUrl, fileFormData, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}

        }).success(function (response) {
            deffered.resolve(response);

        }).error(function (response) {
            deffered.reject(response);
        });

        return deffered.promise;
    }
});

Spring Controller:

@SuppressWarnings({ "unused", "static-access" })
    @RequestMapping( value="/RegisterCandidate" ,  method = RequestMethod.POST)
    private String RegisterCandidate(HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam CommonsMultipartFile[] fileUpload ) throws Exception{

        System.out.println("In method");
        String email = request.getParameter("email");
        System.out.println("email==============="+email);
        String Password = request.getParameter("password");
        String usr_name = request.getParameter("name");
        String mobile_no = request.getParameter("mobile_no");
        Date dateentry = new Date();
        java.sql.Timestamp entry_date = new Timestamp(dateentry.getTime()); 
        Users_Pojo usr  = new Users_Pojo(); 

        if (fileUpload != null && fileUpload.length > 0) {
            for (CommonsMultipartFile aFile : fileUpload){                             
                usr.setFilename(aFile.getOriginalFilename());
                usr.setFile_data(aFile.getBytes());


                System.out.println("aFile.getBytes()======"+aFile.getBytes());
                System.out.println("aFile.getInputStream()======"+aFile.getInputStream());
                System.out.println("aFile.getStorageDescription()======"+aFile.getStorageDescription());
                System.out.println("aFile.getSize();======"+aFile.getSize());
                System.out.println("aFile.getContentType();==="+aFile.getContentType());/*  */


            }
        }
        MD5CodeGenerator md5 = new MD5CodeGenerator();

        usr.setUc_password(md5.convertToMD5(Password));
        usr.setUc_name(email);
        usr.setUc_contact_person(email);
        usr.setUc_phone_no(BigInteger.valueOf(Long.parseLong(mobile_no)));
        usr.setUc_email_id(email);
        usr.setUc_type_id(1);
        usr.setUc_active(1);
        usr.setValid_from(null);
        usr.setValid_to(null);
        usr.setDesignation("jobseekar");
        usr.setIp_address("164.100.19.79");
        usr.setUser_location(1);
        usr.setEntry_date(entry_date);

        scm_service.save(usr, email);

        return "success";

    }

0 个答案:

没有答案