简体   繁体   English

Angular 5文件下载问题-显示index.html内容

[英]Angular 5 file download issue - index.html content shown

I'm trying to download a static file from a location within my code base. 我正在尝试从代码库中的某个位置下载静态文件。 I'm using the following code to achieve this; 我正在使用以下代码来实现此目的;

<div>
    <a [href]="'download/foo.csv'" class="btn btn-default" style="margin-left: 20px;" target="_blank" download="Foo.csv">
        Download Import File Template
    </a>
</div>

However, when file Foo.csv is downloaded its content is same as the index.html file. 但是,下载文件Foo.csv时,其内容与index.html文件相同。 Why is this happening? 为什么会这样呢?

Content of the index.html file is shown below. index.html文件的内容如下所示。

<!DOCTYPE html>
<html lang="en">
<head>
    <base href="/">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Condensed|Source+Sans+Pro">
</head>
<body>
<div app></div>
<script type="text/javascript" src="/api/v2/config?plain=1"></script>
<script type="text/javascript" src="style.200453e6923c58c1d928.js"></script><script type="text/javascript" src="app.83fe6f9a0095f7b1bf2b.js"></script></body>
</html>

Try keeping your CSV file in the assets folder which is where it is supposed to be kept ideally. 尝试将CS​​V文件保存在assets文件夹中,理想情况下应将其保存在该文件夹中。 And then try with this: 然后尝试:

<div>
    <a 
    href="assets/sample.csv" 
    class="btn btn-default" 
    style="margin-left: 20px;" 
    target="_blank" 
    download="sample-file.csv">
        Download Import File Template
    </a>
</div>

Here's a Working Sample StackBlitz for your ref. 这是供您参考的工作样本StackBlitz

PS: The answer to why this works lies in the Building Configuration that is present in the angular.json file which I recommend you checking out. PS:为何angular.json的答案在于angular.json文件中的Building Configuration,我建议您签出该文件。

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

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