Skip to content

Instantly share code, notes, and snippets.

@ridhof
Created November 18, 2019 14:08
Show Gist options
  • Save ridhof/49fe2974090bfc4d012385f0821d86e0 to your computer and use it in GitHub Desktop.
Save ridhof/49fe2974090bfc4d012385f0821d86e0 to your computer and use it in GitHub Desktop.
Upload files on Angular
updateFile(foto_param)
{
const formData: FormData = new FormData()
formData.append('pas_foto', foto_param, foto_param.name);
return this.http.post(''.concat( this.config.server_url ,"mahasiswas/beasiswa/berkas/", ""+this.user_id), formData);
}
<tr>
<td>
<input type="file" nbInput fullWidth (change)="onPasFotoSelected($event)">
</td>
</tr>
onPasFotoSelected(event) {
if(event.target.files.length > 0)
{
const reader = new FileReader();
reader.onload = e => this.pas_foto = reader.result;
reader.onloadend = (e) => {
this.mahasiswa.updateFile(reader.result).subscribe(
(data) => {
console.log(data)
}
)
}
reader.readAsDataURL(event.target.files[0]);
this.pas_foto_name = event.target.files[0].name
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment