Andrew
Andrew

Reputation: 245

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();

Upvotes: 3

Views: 4584

Answers (1)

Andrew
Andrew

Reputation: 245

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

Upvotes: 10

Related Questions