AngularJS - Receive and download CSV

前端 未结 5 900
醉酒成梦
醉酒成梦 2020-12-11 17:11

I am using a nodeJS program as a server and an AngularJS web application as the client.

To create the CSV I\'m using the \"express-csv\" library (https://www.npmjs.c

5条回答
  •  温柔的废话
    2020-12-11 17:23

    I faced same issue that the solutions mentioned above with works well with Chrome and Firefox, but not with safari/IE.

    Tried out following hack and it worked for me-

    var url="http://'+$localStorage.ip+':"+$localStorage.port+'/exportDB'; var form = angular.element("

    "); form.submit();

    File download will be handled by browser itself. though following is limitation to it -

    1. You won't be able to handle the errors (if any) from your http get call :( For this tried using try-catch block of javascript, but didn't work well...
    2. ..you wont be able to unit test this piece of code :( :(

    There is another approach that worked and it was -

    var url="http://'+$localStorage.ip+':"+$localStorage.port+'/exportDB'; $window.location.href = url;

    Suggestions and Discussions welcome!

提交回复
热议问题