Filesaver.js: FileSaver works for me just for .txt

Created on 19 Nov 2015  Â·  9Comments  Â·  Source: eligrey/FileSaver.js

Hi I'm using FileSaver to save my files with different extension but it woks just with .txt
var blob = new Blob([data], { type: contentType });
contentType is a variable here
FileSaver.saveAs(blob, fileName);
for .pdf for example i'm getting failed to load pdf document is there any solution for this !!

Most helpful comment

@jimmywarting For sure you don't know what's wrong without any information. :) I understand your point of view. I'm new to JS and I was pretty frustrated when I saw that someone found a solution but didn't share it.

Having said that, I'm happy that I solved the issue myself. Indeed it was a problem with the http response. The body is sent as 'Text' by default. I had to add a 'responseType', in this case 'ArrayBuffer'. My example:

this.http.get(url, {responseType: ResponseContentType.ArrayBuffer})

The following discussions helped me to figure this out:
http://stackoverflow.com/questions/28093736/failed-to-load-pdf-document-angular-js-blob
http://stackoverflow.com/questions/35138424/how-do-i-download-a-file-with-angular2

PS: As far as I understand, this is specific to Angular2 since I use their http class. But as you said, the important thing is how you get the data.

All 9 comments

It shouldn't matter what content type it's if you are just downloading it.
The content type is only helpful for the browser to render or execute a file correctly.

could you provide some more info like what OS & browser you are using? could you also share the data/file you are trying to save?
also is the data just a simple plain text or is it actually the content of a pdf? could the data be invalid maybe?

for example this wouldn't work cuz it's just plain text and would need to be converted to a pdf format

 saveAs(new Blob(['hello world'], {type: application/pdf}), 'sample.pdf')

Hi @jimmywarting i already found a solution for this,thanks for your reply :)

then we can close this issue :)

yeah thanks :)

2015-12-01 13:49 GMT+00:00 Jimmy Karl Roland Wärting <
[email protected]>:

then we can close this issue :)

—
Reply to this email directly or view it on GitHub
https://github.com/eligrey/FileSaver.js/issues/183#issuecomment-160975041
.

Mounir IGORMAN
Ingénieur Etudes & Développement
Phone : +212617612986

I'm having a similar issues saving a PDF.

@migorman, do you mind sharing your solution?

Thanks!

@theonlysteve is this what you want?

So, I have the same issue. PDF is not working. :/ Does anyone know the solution?

@Gonzo17 can you tell why my car isn't working? No cuz you haven't seen it.

You need to show us some code for us to help you. How do you get the pdf content?

Probably the main cause is that your blob is probably constructed with a string or a binary string. It's nothing wrong with FileSaver but rather who you get the pdf binaries.

@jimmywarting For sure you don't know what's wrong without any information. :) I understand your point of view. I'm new to JS and I was pretty frustrated when I saw that someone found a solution but didn't share it.

Having said that, I'm happy that I solved the issue myself. Indeed it was a problem with the http response. The body is sent as 'Text' by default. I had to add a 'responseType', in this case 'ArrayBuffer'. My example:

this.http.get(url, {responseType: ResponseContentType.ArrayBuffer})

The following discussions helped me to figure this out:
http://stackoverflow.com/questions/28093736/failed-to-load-pdf-document-angular-js-blob
http://stackoverflow.com/questions/35138424/how-do-i-download-a-file-with-angular2

PS: As far as I understand, this is specific to Angular2 since I use their http class. But as you said, the important thing is how you get the data.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

meenucoditas picture meenucoditas  Â·  6Comments

mluis picture mluis  Â·  3Comments

josepoma picture josepoma  Â·  4Comments

rhyous picture rhyous  Â·  6Comments

Toterbiber picture Toterbiber  Â·  5Comments