【问题标题】:How to receive uploaded file from angularjs in grails(backend)?如何在grails(后端)中接收来自angularjs的上传文件?
【发布时间】:2016-11-29 14:19:18
【问题描述】:

我正在前端(angularjs)上传一个excel文件,并希望在后端(grails资源文件)接收它。我不应该在我的项目中使用GSP。那么如何接收通过发送的文件角度js?

AngularJs 代码,

 $scope.uploadExcelFile =function (file) {
        var doc = file
        var filesArr = [];
        if (doc) {
            filesArr.push({file: doc});
            uploadCurry(filesArr);
        }
    }
    function uploadCurry(queue){
        if (arguments.length) {
            this.q = queue
        }
        if (this.q.length) {
            var curr = this.q.pop();
            upload(curr.file);
        }
    }
    function upload(file) {
        if (file) {
            var postdata = {
                file: file,
            }
            roleProfileService.importExcleFile(postdata).then(function(res) {
                console.log(res);
            })
        }
    }

Angular 服务文件,

var importExcleFile = function(file,callback){

       return $upload.upload({
           url: '/api/importFile/profile',
           method: 'POST',
           file: file
       }).progress(function(evt) {
           _log('progress: ' + parseInt(100.0 * evt.loaded / evt.total) + '% file :'+ evt.config.file.name);
           roleProfileService.progress = 'Loading '+ evt.config.file.name +" : "+ parseInt(100.0 * evt.loaded / evt.total)+"%"
       }).success(function(data, status, headers, config) {
           _log('file ' + config.file.name + 'is uploaded successfully. Response: ' + data);
           roleProfileService.uploadsuccess = true;
           if(callback) callback();


       }).error(function(a,b,c,d,e){
           _log("err", a,b,c,d,e);
           roleProfileService.uploadfail = true;
       });
   }

Grails 资源文件,

@Path('/api/importFile')
@Consumes(['application/json','text/plain','application/vnd.ms-excel'])
@Produces(['application/json'])

class KsaResource {
    KsaService ksaService

    @GET
    @Path('profile')
    Response getAllKsaData(MultipartFile file) {
        //how to receive uploaded file here?
        List<Job> jobSavedList = ksaService.importExcelData(file);
        //rest of code
    }
} 

【问题讨论】:

    标签: angularjs grails


    【解决方案1】:

    我们可以写一个控制器来接收文件

    class ImportRoleProfileController {
    
        SpringSecurityService springSecurityService;
    
        def importUpload() {
            User currentUser = springSecurityService.getCurrentUser();
            def file = request.getFile('file')
            Workbook workbook = Workbook.getWorkbook(file.getInputStream());
            int sheet_Num = workbook.getNumberOfSheets();
    

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2019-07-15
      • 2023-02-10
      • 2012-12-27
      • 1970-01-01
      • 2014-11-19
      • 1970-01-01
      相关资源
      最近更新 更多