javascript - How to download files from assets using HttpClient in Angular? - Stack Overflow

Based on previous answers to similar questions such as this one, downloading, or in general, reading fi

Based on previous answers to similar questions such as this one, downloading, or in general, reading files from the assets directory should be a straightforward call to HttpClient's get method. For example if I have a DownloadService, I could simply define it as:

export class DownloadService {
    
  constructor(http: HttpClient){}

  download(data) {
    this.http.get('/assets/path/to/file.ext').subscribe(res => process(res));
  }

}

However, when I test this using ng serve, I get an error saying that /assets/path/to/file.ext is not a valid URL. I tried ./assets/path/to/file.ext, and assets/path/to/file.ext but I got the same error. Do I need to configure something in the runtime to get this working properly, or has this changed since those answers were written? I'm using Angular 9.

Based on previous answers to similar questions such as this one, downloading, or in general, reading files from the assets directory should be a straightforward call to HttpClient's get method. For example if I have a DownloadService, I could simply define it as:

export class DownloadService {
    
  constructor(http: HttpClient){}

  download(data) {
    this.http.get('/assets/path/to/file.ext').subscribe(res => process(res));
  }

}

However, when I test this using ng serve, I get an error saying that /assets/path/to/file.ext is not a valid URL. I tried ./assets/path/to/file.ext, and assets/path/to/file.ext but I got the same error. Do I need to configure something in the runtime to get this working properly, or has this changed since those answers were written? I'm using Angular 9.

Share Improve this question asked Jul 22, 2020 at 14:47 Psycho PunchPsycho Punch 6,93212 gold badges58 silver badges91 bronze badges 2
  • did you console response? what is exact content? – Hien Nguyen Commented Jul 22, 2020 at 14:55
  • I don't think it even gets to the point where I could process the response because it rejects the URL before it even attempts to execute the request. – Psycho Punch Commented Jul 22, 2020 at 14:58
Add a ment  | 

2 Answers 2

Reset to default 3

This error occurs whenever you try to fetch something different than a JSON. To fix it, please define the responseType accordingly. For example

this.http.get('./assets/path/to/file.txt', {responseType: 'text'}).subscribe(res => process(res));

Also, please notice how I've changed the URL, it should be ./assets/foo.bar

Here's a working example https://stackblitz./edit/angular-9-starter-l1cn5j

Just check the console :)

By the way, these are the accepted types for responseType:

responseType: 'arraybuffer'|'blob'|'json'|'text'

I found out that the issue I was experiencing was due to a custom HTTP interceptor that assumes all URLs are valid URLs (file paths aren't). What I ended up doing to address this was to modify the interceptor to ignore URLs that are not valid.

Getting the asset to download using HttpClient's get works as described.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745158855a4614287.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信