简体   繁体   English

如何将本地文件读入字符串

[英]How to read local file into string

I want to read a (svg) file in Angular 6 from assets into a string. 我想将Angular 6中的(svg)文件从资产读入字符串。 I tried things like that: 我尝试过这样的事情:

  this._htmlClient.get('../../assets/images/chart1.svg').subscribe(data => { console.log('svg-file: ', data); }); 

But it does not work. 但它不起作用。 The console.log is not written and I get a error console.log没有写入,我收到错误

ERROR HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: " http://localhost:4200/assets/images/chart1.svg ", ok: false, ERROR HttpErrorResponse {headers:HttpHeaders,status:200,statusText:“OK”,url:“ http:// localhost:4200 / assets / images / chart1.svg ”,ok:false,

The path is right. 路径是对的。 When i copy it into the browser I see my picture. 当我将它复制到浏览器中时,我看到了我的照片。

How I can get the file content into a string? 如何将文件内容转换为字符串?

By default, the HttpClient expects the response to be JSON. 默认情况下,HttpClient期望响应为JSON。 If you want to get something else, you need to specify the type: 如果您想获得其他内容,则需要指定类型:

this._htmlClient.get('path', { responseType: 'text' })
  .subscribe(data => { console.log(data) });

HttpClient - Requesting Non-JSON Data HttpClient - 请求非JSON数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM