lundi 26 juin 2017

How to send an object with a file in it via rest API in Angular 2?

here is my service function:

updateCollect(id: number, data: any) {
    let token = 'bearer ' + this.cookie.get('token');
    let dataToSend = new FormData();
    dataToSend.append('value', data.value);
    dataToSend.append('comment', data.comment);
    dataToSend.append('proofs', JSON.stringify(data.proofs));
    const headers = new Headers({'enctype': 'multipart/form-data', 'Authorization': token});
    return this.http.post(this.globals.COMPLETE_COLLECT_URI + '/' + id, dataToSend, {
        headers: headers
    });
}

I try to post it everything is good here is what I get on my back end (laravel)

array:3 [
  "value" => "2000"
  "comment" => "null"
  "proofs" => "[{"name":"proof 2","file":{}}]"
]

the problem is the file object is always empty, any idea why ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire