angulartypescriptspring-bootangular-file-uploadmultipartfile

angular Spring Boot File Upload Example


I want to Upload File To Spring boot backend , How should I send Formdata as requestparam in postrequest, i tried following code

angular service class code

let headers=new HttpHeaders({
               'Content-Type="multipart/form-data'
                })
                const formData:FormData=new FormData();
                formdata.apend('file',file);
                 return this.http.post(uploaadfile,{params:{
                 file:formData,
                  contracttemplateid:28
                  }},options);

spring boot controller code

@postmapping
uploadfile(@requestparam("file") MultipartFile 
file,@requestparam("contracttemplateid") long id)
{
}

Solution

  • Uploadfile(formData: FormData, FileName: string){    
       return this.http.post('URL',formData,{'params': {filename : FileName}});
      }
    
    let fd = new FormData();
    let FileName = "FileName";
    
    fd.append(SeletedFile, FileName)
    
    Uploadfile(fd,FileName);