Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve blob and have good filename for all users?

I have a PDF file as a blob object. I want to serve to my users, and right now I'm doing:

html = '<iframe src="' + URL.createURL(blob) + '">'; 

That works fine for people that want to use their in-browser PDF tool.

But...some people have their browser set to automatically download PDFs. For those people, the name of the downloaded file is some random string based on the blob URL. That's a bad experience for them.

I know I can also do:

<a href="blobURL" download="some-filename.pdf">

But that's a bad experience for the people who want to use in-browser PDF readers, since it forces them to download the file.

Is there a way to make everybody have good file names and to allow everybody to read the PDF the way they want to (in their browser or in their OS's reader)?

Thanks

like image 974
mlissner Avatar asked Nov 11 '17 23:11

mlissner


Video Answer


2 Answers

At least looking at Google Chrome, if the user disables the PDF Viewer (using the option "Download PDF files instead of automatically opening them in Chrome") then window.navigator.plugins will show neither "Chromium PDF Plugin" nor "Chromium PDF Viewer". If the option is left at the default setting, the viewer will show in the plugin list.

Using this method, one can utilize window.navigator.plugins to check if any of the elements' names are either of the aforementioned plugins. Then, depending upon that result, either display a <iframe> or a <a href="blobUrl" download="file.pdf">. For other browsers I imagine that different methods would have to be used. You can also check for a "Acrobat Reader" plugin, which some machines may have instead, or even just the word "PDF".

On a side note, it does look like it is possible to detect if the default Firefox PDF viewer is enabled by using http://www.pinlady.net/PluginDetect/PDFjs/ .

like image 101
Greenbeard Avatar answered Sep 28 '22 12:09

Greenbeard


Try to append &filename=thename.pdf to the binary, metadata or http header:

Content-Disposition: attachment; filename="thename.pdf"

like image 28
user5886944 Avatar answered Sep 28 '22 13:09

user5886944