download csv file from web api in angular js

前端 未结 10 1741
[愿得一人]
[愿得一人] 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:20

    I think the best way to download any file generated by REST call is to use window.location example :

        $http({
            url: url,
            method: 'GET'
        })
        .then(function scb(response) {
            var dataResponse = response.data;
            //if response.data for example is : localhost/export/data.csv
            
            //the following will download the file without changing the current page location
            window.location = 'http://'+ response.data
        }, function(response) {
          showWarningNotification($filter('translate')("global.errorGetDataServer"));
        });

提交回复
热议问题