PDF media file is downloaded instead of shown in the browser

I have pdf media files stored in a data table. I then use Links in a repeating table to show the user a preview of the pdf.
I know that by default the media files are downloaded instead of show in a (new) browser window. But there is a -still- undocumented method media.get_url(False) to get a link that should yield a non downloading url. See this discussion:

[SOLVED] PDF Media Files not previewing in browser

In effect, it seems to append “&nodl=1” to the url.

And indeed I have used this technique in the past, but it does not seem to work anymore. Even if I manually paste the non-downloading url in a browser window, the file still downloads. I tested this in Safari and Chrome.

Any idea what is going on?

Investigating further I noticed that the mime-type looked weird for the pdf file:
Screenshot 2020-08-03 at 12.23.41

I upload the pdf files with an uplink program, and there I found the culprit:

anvil.media.from_file(str(d['path']),
                                  mime_type=mimetypes.guess_type(d['path']))

Turns out the guess_type function returns a tuple (type, encoding). I fed the whole tuple to anvil.media.from_file, which it accepted, but is obviously wrong. Changing the code to:

anvil.media.from_file(str(d['path']),
  mime_type=mimetypes.guess_type(d['path'])[0])

fixes the problem. So it seems that a wrong mime-type can really mess up things.

Hope this helps somebody (including future me).

3 Likes