Unpoly 2: Don't try to download files through AJAX requests

Posted . Visible to the public.

Rails has the handy controller method send_file Show archive.org snapshot which lets us download files easily. We can decide whether the file should be downloaded (disposition: 'attachment') or shown in the browser (disposition: 'inline'). The default is disposition: 'attachment'.

Downloading files will not work when you are calling the controller action from an AJAX request. The browser will try to render the file and insert it in the DOM, which is never what you want.

Unpoly 2

Unpoly (since version 2) can be configured to follow all links (or submit all forms, ...) without having to specify the selector on every link, as described here: https://unpoly.com/up.link.config Show archive.org snapshot .

Make sure to always exclude your download links, otherwise your downloads won't work as expected.

By default will not follow links that have the standard [download] attribute Show archive.org snapshot . This attribute has the same effect as a Content-Disposition: attachment header sent by the server.

<a href="..." download>

You can also tell Unpoly to treat that one link as a vanilla Javascript link:

<a href="..." up-follow="false">
Jakob Scholz
Last edit
Henning Koch
Keywords
unpoly, 2, download
License
Source code in this card is licensed under the MIT License.
Posted by Jakob Scholz to makandra dev (2021-09-14 07:29)