Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了AngularJS上传文件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

上传文件

  • html
<div>
    <input id="fileUpload" type="file" />
    <button ng-click="uploadFile()">上传</button>
</div>
  • js
$scope.upload = function(){
            var form = new FormData();
            var file = document.getElementById("fileUpload").files[0];
            fd.append('file',filE);
            $http({
                method: 'POST',url: '/upload',data: form,headers: {'Content-Type': undefineD},transformrequest: angular.identity
            }).success(function (data) {
                console.log('upload success');
            }).error(function (data) {
                 console.log('upload fail');
            })
        }
@requestMapping("/upload")
    public void uploadFile(@requestParam(value = "file",required = true) multipartfile filE) {
        //deal with file
    }

上传文件的同时传递其他参数

  • html
<div>
        <input id="fileUpload" type="file" />
        <button ng-click="ok()">上传</button><br>
        <input ng-model="user.username" />
        <input ng-model="user.password" />
    </div>
  • js
$scope.ok = function () {
        var form = new FormData();
        var file = document.getElementById("fileUpload").files[0];   
        var user =JSON.Stringify($scope.user);

        form.append('file',filE);
        form.append('user',user);

        $http({
            method: 'POST',url: '/addUser',transformrequest: angular.identity
        }).success(function (data) {
            console.log('operation success');
        }).error(function (data) {
            console.log('operation fail');
        })
    };
@requestMapping("/upload")
    public Map<String,Object> upload(@requestParam(value = "file") multipartfile file,@requestParam(value = "user",required = true) String user) {

        try (FileInputStream in = (FileInputStream) headImg.geTinputStream();
             FiLeoutputStream out = new FiLeoutputStream("filePathAndName")) {

            //将Json对象解析为UserModel对象
            ObjectMapper objectMapper = new ObjectMapper();
            UserModel userModel = objectMapper.readValue(user,UserModel.class);

            //保存文件到filePathAndName
            int hasRead = 0;
            byte[] bytes = new byte[1024];
            while ((hasRead = in.read(bytes)) > 0) {
                out.write(bytes,0,hasRead);
            }
        } catch (IOException E) {
            e.printStackTrace();
        }
    }

大佬总结

以上是大佬教程为你收集整理的AngularJS上传文件全部内容,希望文章能够帮你解决AngularJS上传文件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。