typescript - Hur skickar man en bild som finns i Angulars assets-mapp i ett Angular-projekt till backend?
Tillgångsbild som jag vill skicka till backend
Hur man skickar en bild till backend som jag redan har i mitt Angular-projekt, jag vill inte ladda upp från HTML.
Detta är koden jag har skrivit, men den fungerar inte!!
fileToUpload: File = new File(['assets/layout/images/register_user.png'], 'register_user.png');
private submitRegistration(){
this.registerUser.registerUser(this.fileToUpload, this.registerForm.value).subscribe();
}
Svar #1
Du måste ladda det med hjälp av http-klienten.
Den enklaste lösningen skulle vara:
import { HttpClient } from '@angular/common/http';
import { map } from 'rxjs/operators';
constructor(http: HttpClient) {
const fileName = 'register_user.png';
const path = '/assets/layout/images/' + fileName;
const file$ = http.get(path, { responseType: 'blob' })
.pipe(map(blob => new File([blob], fileName)));
file$.subscribe(file => /*do something with file*/);
}
