2017-06-06 66 views
1

我發送給後端文件,這是下面的代碼上傳:Recieving上傳從客戶端文件,休息

export class FileUploadComponent { 

    @Input() multiple: boolean = false; 
    @ViewChild('fileInput') inputEl: ElementRef; 

    constructor(private http: Http) {} 

    upload() { 
    let inputEl: HTMLInputElement = this.inputEl.nativeElement; 
    let fileCount: number = inputEl.files.length; 
    let formData = new FormData(); 
    if (fileCount > 0) { // a file was selected 
     for (let i = 0; i < fileCount; i++) { 
     formData.append('file[]', inputEl.files.item(i)); 
     } 
     this.http 
     .post('http://localhost:8080/upload', formData).toPromise().then(() => console.log('success')).catch(() => console.log('error')); 
    } 
    } 
} 
現在

上,我想通過控制器來接受她的backendside,但我不知道如何映射文件屬性,以下給出null:

public @ResponseBody String handleFileUpload(@RequestBody MultipartFile file) 
+0

[AngularJS FORMDATA文件上傳數組(可能的重複https://stackoverflow.com/問題/ 33920248/angularjs-FORMDATA文件陣列上傳) –

回答

1

您的方法簽名不正確。

@RequestBody註釋參數映射到HTTP請求主體。

@RequestParam註解參數映射到特定的Servlet請求參數。

使用以下命令:

public @ResponseBody String handleFileUpload(@RequestParam MultipartFile file) 

如果要發送多個,然後使用數組:

public @ResponseBody String handleFileUpload(@RequestParam MultipartFile[[] file)