0

I'm making an APP of my homepage. And I got a problem in making a 'Writing page'. I have to POST some data and image. And I can't get how to POST image!

I searched and found this code. but it's still not working... I think the FormData is Empty. Can you tell me why the FormData is empty or Can you show me a better way to POST Image and Texts in Ionic 2?

This is my HTML code..

<input type="file" (change)="write($event)"/>

and my TypeScript code

write(event) {

  let file = event.srcElement.files.item(0);

  let formData: FormData = new FormData();

  let headers = new Headers({
    'Content-Type': 'multipart/form-data'
  });

  let options = new RequestOptions({
    headers: headers
  });

  formData.append('bf_file', file, file.name);
  formData.append('wr_subject', 'Just Testing');
  formData.append('wr_content', 'Some content...');
  formData.append('bo_table', 'table');

  this.http.post('http://www.myhomepage.com/write_update.php', formData, options).subscribe(res => {
    console.log(res.text());
  });

}

Please help me...

Daniel
  • 521
  • 1
  • 7
  • 16
  • This [other thread](http://stackoverflow.com/a/40216616) says you need to retrieve the file from `event.target.files[0]`. Also, are you sure the problem is not with `write_update.php`? Try sending the request to a debugging service like `https://httpbin.org/post` and see what it returns. – AngularChef Feb 18 '17 at 11:57
  • @AngularFrance Thanks, I will try it... – Daniel Feb 19 '17 at 06:28

0 Answers0