I'm working in ASP.NET MVC in Visual Studio, and when I click a button on the Index.html page, it is supposed to initiate a file download.
The file structure is:
~/Layer1/Layer2/(Index.html and Data.csv).
I have the href pointing to "Data.csv", the file I want to download. Instead of downloading the contents of the file, it reads it as another webpage and downloads the HTML page .../Index/Data.csv. So when it opens in excel there's a bunch of html content, not the csv content of the file.
Edit. Here's the a tag (using Knockout for the data-bind):
<a class="btn" data-bind="click: exportToCSV" download>EXPORT TO CSV</a>
The exportToCSV function modifies makes a call to a function, modifies the csv file, and then does:
var link = document.createElement("a");
link.download = "Data.csv";
link.href = "Data.csv";
link.click();
That creates a new element with the reference to the file and "clicks" it.
Aucun commentaire:
Enregistrer un commentaire