-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Customize @@display-file to allow to download files with proper filename
- Loading branch information
Showing
3 changed files
with
40 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from plone.namedfile.browser import DisplayFile as BaseView | ||
from urllib.parse import quote | ||
|
||
|
||
class DisplayFile(BaseView): | ||
""" | ||
Custom view | ||
""" | ||
|
||
def set_headers(self, file): | ||
""" | ||
backport of https://github.com/RedTurtle/redturtle.volto/pull/113 | ||
We need to add filename to the reponse because otherwise the browser | ||
use the field name as filename (last path element). | ||
content-disposition should be "inline" to allow to display the file in the browser | ||
without forcing download (with "attachment" the browser will download it). | ||
""" | ||
super().set_headers(file=file) | ||
|
||
filename = getattr(file, "filename", "") | ||
if filename is not None: | ||
if not isinstance(filename, str): | ||
filename = str(filename, "utf-8", errors="ignore") | ||
filename = quote(filename.encode("utf8")) | ||
self.request.response.setHeader( | ||
"Content-Disposition", f"inline; filename*=UTF-8''{filename}" | ||
) |