【问题标题】:Save file uploaded using angular-file-upload保存使用 angular-file-upload 上传的文件
【发布时间】:2016-02-04 09:59:28
【问题描述】:

我创建了一个使用 Angular 文件上传组件 (https://github.com/nervgh/angular-file-upload/wiki/Module-API) 的页面,但是我找不到如何获取上传的文件信息以将其保存在磁盘上。

我不知道密钥名称是什么以及如何保存它。如果您能帮助我,这是我的代码示例。

app.controller('HomeController', ['$scope', 'FileUploader', function ($scope, FileUploader) {
    $scope.uploadMessage = 'Arraste aqui o arquivo';

    var uploader = $scope.uploader = new FileUploader( { autoUpload: true, url: '/url'});

    // FILTERS

    uploader.filters.push({
        name: 'customFilter',
        fn: function(item /*{File|FileLikeObject}*/, options) {
            return this.queue.length <= 1;
        }
    });

    // Callbacks
    uploader.onAfterAddingFile = function (item) {
        $scope.uploadMessage = 'Enviando arquivo ' + item.file.name.toString();
    };

    uploader.onCompleteItem = function(item, response, status, headers) {

    };
}]);

而我的servlet post函数是这样的:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

Angular 组件帮助说有一个名为“alias”的属性 {String}:将包含文件的字段的名称,默认为文件。如果我使用 request.getParameter("file") 它是空的。

更新

我在我的服务器中使用 @MultipartConfig 注释,但是 request.getPart 始终为空。

【问题讨论】:

标签: angularjs servlets servlet-3.0


【解决方案1】:

需要更改您的网址,它应该是唯一的。 试试这个

var uploader = $scope.uploader = new FileUploader({ autoUpload: true, url: '/upload' });

服务器端:

@RequestMapping(value = "/upload" , method = RequestMethod.POST)
public void upload(HttpServletRequest request)
{
    //org.springframework.web.multipart.MultipartHttpServletRequest
    MultipartHttpServletRequest mRequest;
    mRequest = (MultipartHttpServletRequest) request;

    Iterator<String> itr = mRequest.getFileNames();
    while (itr.hasNext()) {
        //org.springframework.web.multipart.MultipartFile
        MultipartFile mFile = mRequest.getFile(itr.next());
        String fileName = mFile.getOriginalFilename();
        System.out.println("*****"+ fileName);

        //To copy the file to a specific location in machine.
        File file = new File('path/to/new/location');
        FileCopyUtils.copy(mFile.getBytes(), file); //This will copy the file to the specific location.
    }
}

【讨论】:

  • 我已经在使用一个唯一的 url。我是否必须添加 Spring 依赖才能使其正常工作?
猜你喜欢
  • 1970-01-01
  • 2015-12-01
  • 1970-01-01
  • 2017-10-20
  • 1970-01-01
  • 2020-01-15
  • 2018-02-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多