Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular: Load SVG from path and stringify it

I need to load an svg file from the assets folder and stringify the content. Can't find any solution int the web but here is my approach.

    let svg = await this.httpClient.get(`assets/images/result/${this.icon}.svg`)
.pipe(
      map(res => {
        const serializer = new XMLSerializer();
        const svgString = serializer.serializeToString(<any>res);
      })
    ).toPromise();
like image 926
Andrew Avatar asked Jun 11 '26 10:06

Andrew


1 Answers

i figured it out.

const headers = new HttpHeaders();
headers.set('Accept', 'image/svg+xml');
const svgString =
  await this.httpClient.get(`assets/images/result/${this.icon}.svg`, {headers, responseType: 'text'}).toPromise();

important is that you define the header with accept "image/svg+xml" and as responseType "text

like image 89
Andrew Avatar answered Jun 13 '26 23:06

Andrew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!