Angular 2文件上传形式和Java

rig*_*gby 5 java file-upload angular

我在Angular2中有前端,在Java中有后端.我使用Java并且使用Angular发现这个任务有点棘手.

我需要在表单中上传文件.如何将文件添加到表单对象,就像字段值的其余部分一样.它有可能吗?

我的意思是这样的

onSubmit() {
        if (this.isSubmitting)
            return;

        this.isSubmitting = true;

        let newService = {
            name: this.editServiceForm.value.name,
            summary: this.editServiceForm.value.summary
            logo: this.editServiceForm.value.logo <-----------like this
        };
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我得到空字符串的标志.所有其他"正常"字符串文件都是正确的.

我在StackOverflow上看到了很多文件上传的解决方案,他们都描述了在单独的请求中上传文件.像这样的模板: <div> <input type="file" (change)="onChange($event)"/> </div> ,

.....

onChange(event) {
    console.log('onChange');
    var files = event.srcElement.files;
    console.log(files);
    this.service.makeFileRequest('http://localhost:8182/upload', [], files).subscribe(() => {
      console.log('sent');
    });
  }
Run Code Online (Sandbox Code Playgroud)

在this.service.makeFileRequest中我们发送一个专门用于文件上传的请求.

如果我这样做,我需要单独处理此请求,然后单独处理表单中的其他数据(如名称和摘要).如果是这样,在java中处理这个问题的正确方法是什么?