简体   繁体   English

angular2多部分图片上传

[英]angular2 multipart image upload

I'm developing web application with spring rest and angilar2 . 我正在使用spring restangilar2开发Web应用程序。 I have such backend api (in pseudocode): 我有这样的后端api(伪代码):

@POST
method create( @Json Data data, @Multipart Image img){
  ...........
}

I need an angular2 example relevant to this particular api. 我需要一个与这个特定api相关的angular2示例。 Is it possible to send JSON and Image separately in one request? 是否可以在一个请求中单独发送JSON和Image? Maybe I have to send Image within JSON as byte[] ? 也许我必须在JSON中发送Image作为byte [] Either have to send two separate requests with data and image as below?: 要么必须发送两个单独的数据和图像请求,如下所示?:

@POST
method create(@Json Data data){
   ........ 
   return Id;
}

@POST
method uplodImg(Long id, Image img){
}

You can use FormData to send data as well as image together in one request. 您可以使用FormData在一个请求中一起发送数据和图像。 For eg:- 例如: -

let formData = new FormData();
        formData.append("name", value);
        formData.append("file",this.file);

And you can access the file as: 您可以访问该文件:

@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

public String uploadLayoutFile(@RequestParam("file") MultipartFile multipartFile, @RequestParam("name") string name)
{
.
.

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM