使用 Angular5 下载文件

CBa*_*arr 1 download angular angular5

我有一个允许上传文件的 Angular 5 项目,我想给用户一个他们可以先下载的示例/模板文件。

文件结构

file-importer/
  | file-importer.component.html
  | file-importer.component.ts
  | example-file-template.csv
Run Code Online (Sandbox Code Playgroud)

在那个 HTML 文件中,我想做这样的事情

<a href='example-file-template.csv' download>
  Download a sample template here
</a>
Run Code Online (Sandbox Code Playgroud)

但是那个链接是不正确的,那么我应该如何链接到这样的静态文件呢?

Jer*_*lle 5

将您的静态文件放在/assets文件夹中。它是 Angular 中的一个特殊文件夹,您应该在那里放置所有静态文件、图像等。然后像这样链接它们:

<a href='assets/example-file-template.csv' download>
  Download a sample template here
</a>
Run Code Online (Sandbox Code Playgroud)

  • 没有为我工作!下载的文件替换为 index.html 的内容 (2认同)