download csv file from web api in angular js

前端 未结 10 1745
[愿得一人]
[愿得一人] 2020-11-28 03:23

my API controller is returning a csv file as seen below:

    [HttpPost]
    public HttpResponseMessage GenerateCSV(FieldParameters fieldParams)
    {
                


        
10条回答
  •  春和景丽
    2020-11-28 04:06

    Try it like :

    File.save(csvInput, function (content) {
        var hiddenElement = document.createElement('a');
    
        hiddenElement.href = 'data:attachment/csv,' + encodeURI(content);
        hiddenElement.target = '_blank';
        hiddenElement.download = 'myFile.csv';
        hiddenElement.click();
    });
    

    based on the most excellent answer in this question

提交回复
热议问题