【问题标题】:Multi file upload with metadata with relation to every file in a single http request与单个 http 请求中的每个文件相关的元数据的多文件上传
【发布时间】:2016-03-20 10:15:58
【问题描述】:

我正在开发一项服务(前端和后端),该服务允许将多个文件和元数据上传到每个文件,因此我将能够生成保存文件的特定路径。 (元数据会给我关于路径的所有信息)。此外,请求将包含需要保存到数据库的信息(这部分工作正常),所以它都是事务性的。所以我唯一想不通的是如何向每个文件发送和接受元数据。

前端:Angularjs

后端:Groovy/Java、Spring 3.0

@RequestMapping(value = "/view/single/multi-file", method = POST, produces = APPLICATION_JSON_VALUE)
def createNewAdjustment(@RequestParam("files") List<List<MultipartFile>> files,
                        @RequestParam("data") List<DataRequest> data){}

所以在上面的代码中:

- the list of lists: are the files that needs to be processed and saved.
- the data: is the list of objects that needs to be processed,saved and interconnect them with files.

请求负载如下所示:

------WebKitFormBoundaryPve8x58T1pAIsQOS
Content-Disposition: form-data; name="data"

{"rollNumber":"1111111111111","compId":213131,"adjId":"260b018c-5921-4c1c-aa99-ba8587ee4777"}
------WebKitFormBoundaryPve8x58T1pAIsQOS
Content-Disposition: form-data; name="files"; filename="some.json.gz"
Content-Type: application/x-gzip


------WebKitFormBoundaryPve8x58T1pAIsQOS
Content-Disposition: form-data; name="files"; filename="someother.csv.gz"
Content-Type: application/x-gzip


------WebKitFormBoundaryPve8x58T1pAIsQOS--

您可以看到 'name: "file"' 重复了 2 次,我不确定如何手动设置或/以及将更多内容附加到请求中。有什么想法吗?

【问题讨论】:

    标签: angularjs spring http file-upload groovy


    【解决方案1】:

    我找到了解决办法:)

    所以从前端(AngularJS):

    $upload.upload({
        url : 'upload',
        headers: {'myHeaderKey': 'myHeaderVal'},
        data : {
            myModel : $scope.myModel,
            array: [1,2,3]
        },
        formDataAppender: function(fd, key, val) {
            if (angular.isArray(val)) {
                angular.forEach(val, function(v) {
                    fd.append(key, v);
                });
            } else {
                fd.append(key, val);
            }
        },
        file : $file,
        fileFormDataName: 'myFile'
    })
    

    formDataAppender 将为请求添加一个新部分,您可以为您发送的每个文件自定义名称。 (正是需要的)。

    从后端(Spring Groovy):

    @Autowired DbRepository dbRepo
    @RequestMapping(value = "/docs/upload", method = POST)
    ResponseEntity<String> docsUpload(HttpServletRequest request, @RequestParam("data") String data) throws IOException {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request
        List<List<MultipartFile>> files = new ArrayList<List<MultipartFile>>()
        multipartRequest.getFileNames().each {
            files.add(multipartRequest.getFiles(it))
        }
        PoJoObject pojoObject = mapper.readValue(data, PoJoObject)
    
        files.each { def it ->
          it.each { def itf ->
            log.debug(itf.originalFilename) // the name of the actual file
            log.debug(itf.name) // the custimized field from formDataAdapter
    
            // in here you can process every field in 'PoJoObject' and make
            // relation to the the file
          }
        }
    
        return null;
    }
    

    此解决方案适用于我并解决了我遇到的问题 :) 现在我可以在单个请求中以事务方式保存数据和相应文件。

    【讨论】:

      猜你喜欢
      • 2019-12-23
      • 2018-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-30
      • 2013-02-02
      • 2012-05-31
      相关资源
      最近更新 更多