0

I'm trying to upload a file using Angular (currently Angular 4). But I'm unable to send it correctly. The best I can do is sending the file as a raw content (available in $_POST as some binary mess like this: )

ëPNG\r\n \x1A\n \x00\x00\x00\rIHDR\x00\x00\x00©\x00\x00\x008\x08\x06\x00\x00\x00Nâ2\x1F\x00\x00\n óiCCPICC

What I want is the file to be uploaded and available in $_FILES variable in PHP.

Currently, I'm using this code in Angular

let picture = event.target.files[0];
let content = new FormData();
content.append('picture', picture, picture.name);
let headers = new Headers({'Accept': 'application/json',
    'X-AUTH-TOKEN': this.user.token,
    'Content-Type': picture.type});
let options = new RequestOptions({headers: headers});
this.http.post(Config.API_URL + `/user/profile/update/`, value, options).subscribe(response => {
    console.log(response);
});

Currently, PHP code is just a var_dump($_FILES);

I think my problem may be related to the way Angular transforms the request. I know there war a transformRequest option in AngularJS but I can't find how it works with Angular 4.

Do you have any suggestion on how I can make this work?

Thanks.

chindit
  • 919
  • 2
  • 9
  • 19
  • Can you look at http://stackoverflow.com/questions/40479053/add-simple-validate-to-form-file-upload-in-angularjs/40479124#40479124 – Jigar7521 May 05 '17 at 06:09
  • 1
    Please see angular2 file uploading: http://stackoverflow.com/questions/35985347/how-to-upload-file-in-angular2/40595005#40595005 – Bharat Chauhan May 05 '17 at 09:11
  • Yesssss! Removing `Content-Type` in header and renaming `picture` by `file` in _FormData_ make the upload work! Thanks a lot. But I don't really understand why such a small modification can lead to the file not being present in `$_FILES`. – chindit May 05 '17 at 16:08

0 Answers0