Файловый объект не будет захвачен.ошибка: SyntaxError: неожиданный токен r в JSON в позиции 0 в JSON - PullRequest
0 голосов
/ 05 июня 2019

Хорошо, у меня есть форма с несколькими полями ввода и полем загрузки файла. У меня проблемы с захватом файла и отправкой его на мой сервер. Я использую Angular на стороне клиента и SpringBoot для сервера. Я занимаюсь этим уже более двух дней, но безуспешно.

Вот мой угловой код:

file.component.ts (я пропустил код для других форм ввода)

onFileChange(event)  {
        const fileList: FileList = event.target.files;
        const file: File = fileList[0];
        console.log(this.file);
      }
   private prepareSave(): any {
    const input = new FormData();
    input.append('issue', this.issueInformation);
    input.append('granularity', this.granularityInformation);
    input.append('file', this.file);
    console.log(this.issueInformation);
    console.log(this.granularityInformation);

    return input;
  }

submitData() {
        const formModel = this.prepareSave();
        console.log(formModel);
        this.submissionService.submit(formModel);
}

}

file.component.html

  <div class="form-group">
      <input type="file"  id="file"  (change)="onFileChange($event)" accept=".csv" #csvFile/>
  </div>

submission.service.ts

export class SubmissionService {
    url =  'http://localhost:8080/uploadFile';
 private options = { headers: new HttpHeaders({ 
     'Content-Type': 'application/json',  'enctype': 'multipart/form-data'
 }) };

    constructor(private http: HttpClient) { }
    submit(submitData: any) {
        this.http.post<any>(this.url, JSON.stringify(submitData), 
        this.options).subscribe(r=>{JSON.parse(r)});
    }
}

Я получаю ошибку:

image

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...