AngularJS 通过 Spring Restful 上传文件

本文介绍了一种使用AngularJS实现文件上传的方法,并详细展示了JS端的指令和服务编写过程,以及Spring后端如何配置和接收上传文件。

总结一下,在写下这些文字之前心里很不爽,一个小问题倒腾了这么久...

 

 JS 端:

  

// 指令
app.directive('fileModel', ['$parse', function($parse) {
        return {
            restrict: 'A',
            link: function(scope, element, attrs) {
                var model = $parse(attrs.fileModel);
                var modelSetter = model.assign;

                element.bind('change', function() {
                    scope.$apply(function() {
                        modelSetter(scope, element[0].files[0]);
                    })
                })
            }
        }
    }])
// Service
app.service('fileUpload', ['$http', function($http) {
        this.uploadFile = function(file, url) {
            var fd = new FormData();
            fd.append('name', file.name);
            fd.append('file', file);

            $http.post(url, fd, {
                transformRequest: angular.identity,
                headers: {'Content-Type': undefined}
            })
            .success(function(data, status) {

            })
            .error(function(data, status) {

            })
        }
    }])
// Controller
app.controller('FileUploadModalCtrl', ['$scope', '$uibModalInstance', 'fileUpload', function($scope, $uibModalInstance, fileUpload) {
        
        $scope.ok = function() {
            var file = $scope.modelFile;
            console.log('file is ' + file);

            var uploadUrl = './api/file/upload';
            fileUpload.uploadFile(file, uploadUrl);

            $uibModalInstance.close($scope.items);
        };
        $scope.cancel = function() {
             $uibModalInstance.dismiss('cancel');
        }
    }]).controller('UploadCtrl', function($scope, $uibModalInstance, modelSrc, imgSrc) {
        $scope.model_src = modelSrc;
        $scope.img_src = imgSrc;

        
    });

 

<!-- HTML -->
<
div class="form-group"> <label for="inputFile">File Input</label> <input type="file" file-model="modelFile"> </div>

 

 

Spring 端

 
  

 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("file")
public class FileUploadService {

    @RequestMapping(method = RequestMethod.POST, value = "/upload")
    public String handleFileUpload(@RequestParam("name") String name, @RequestParam("file") MultipartFile file) {
        
        // todo what you want
    }
}

 

<!-- 这个千万别忘了,在spring配置文件里声明 -->
<
bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

 

 

注释就不加了,下班了 :) 

 

转载于:https://www.cnblogs.com/xbingxin/p/5369214.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值