downloading xlsx file in angular 2 with blob

前端 未结 6 759
悲&欢浪女
悲&欢浪女 2021-01-02 02:31

I want to download xlsx file from the client on angular 2 using rest api.

I\'m getting a byte array as a response from my GET request and I\'m sending it to download

6条回答
  •  佛祖请我去吃肉
    2021-01-02 03:05

    The following solution worked for me in Google Chrome Version 58.0.3029.110 (64-bit).

    It's an article explaining how to download a pdf using a Blob: https://brmorris.blogspot.ie/2017/02/download-pdf-in-angular-2.html

    It's the same principle for a xlsx file. Just follow the steps from the article and change two things:

    • Header, instead of {'Accept': 'application/pdf'}, use {'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'};
    • Blob, use the same type for the creation of your Blob, new Blob([fileBlob], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}).

    The article is using File Saver but I didn't. I left a comment, in the article, with a basic explanation of what I used instead of File Saver.

提交回复
热议问题