I need to upload a file in angle 2 but without having to convert it to base64 and send it as a string, somebody knows if the angular / http library supports FormData to be able to upload a file, if not, that another alternative I have.
Greetings
In the end I managed to find the solution of how to send a FormData by sending it inside the body, it is clear that the first versions of angular2 did not support form data, hence my question, and you had to resort to alternative solutions such as using XHTTP requests raw
template.html
<input type="file" (change)="onArchivoSeleccionado($event)">
component.ts
public onArchivoSeleccionado($event) {
if ($event.target.files.length === 1) {
this.subirArchivo($event.target.files[0]).subscribe(response => {
// respuesta
},
error => {
console.error(error);
});
}
}
public subirArchivo(file): Observable<string> {
let formData = new FormData();
formData.append('file', file);
formData.append('fileName', file.name);
const url = "http://"; //destino en el servidor
const headers = new Headers({});
let options = new RequestOptions({ headers: headers });
return this.http.post(url, formData, options).map(response => response.text());
}