flask-autoindex
flask-autoindex copied to clipboard
How to force download instead of open in browser?
from flask import Blueprint
from flask_autoindex import AutoIndexBlueprint
.
.
.
bp_ai = Blueprint('foo', __name__, static_folder='./static', template_folder='templates')
AutoIndexBlueprint(bp_ai, browse_root=f'{my_dir}',template_context=dict(x='y'))
.
.
.
app.register_blueprint(bp_ai, url_prefix='/foo/files')
This is how i am using AutoIndex. It Works. But clicking on files directly open in browser. How do i enable force download on click?
In doc, i found no information about "as_attachment" parameter. What should i modify in those two lines to enable force download?
Please Help !
Hello,
I can't reproduce issue, in my computer I download files successfully.
You can configure your browser to force download file
Here is the full code if you want to test it. Added some attachments.
import pathlib
from flask import Blueprint, Flask, send_file
from flask_autoindex import AutoIndexBlueprint
app = Flask(__name__)
foo_dir = pathlib.Path(r'C:\foo')
bp_ai = Blueprint('foo', __name__, static_folder='./static', template_folder='templates')
AutoIndexBlueprint(bp_ai, browse_root=f'{foo_dir.absolute()}', template_context=dict(x='y'))
app.register_blueprint(bp_ai, url_prefix='/ai')
sample_mp4_file_path = foo_dir / 'sample-mp4-file.mp4'
@app.route("/browser_ok_mp4_as_attachment_true")
def x():
return send_file(f'{sample_mp4_file_path.absolute()}', as_attachment=True)
@app.route("/browser_ok_mp4_as_attachment_false")
def y():
return send_file(f'{sample_mp4_file_path.absolute()}', as_attachment=False)
if __name__ == '__main__':
app.run()
Test video is from here As you can see its not browser issue. Browser will download file instead of opening if provided as_attachment=True in send_file. I found no way to provide this parameter in AutoIndex.
The as_attachment=True
parameter allow to add Content-Disposition
header to attachment
so to download file as fai can do.
I think your issue title do not reflect that you want !
Your browser treat the header to do something : https://stackoverflow.com/a/1395173/2641416
Oh... I See. Thanks.
Instead of opening a new issue or editing the title, I am just requesting here the feature request.
Please add an option to pass as_attachment=True
parameter or something similar to AutoIndex, for adding Content-Disposition header to attachment.
Ex. During init, AutoIndexBlueprint(..., as_attachment=True)
or,
Or, in config, AUTOINDEX_AS_ATTACHMENT = True
Your request is to configure globally the path to download all files inside ?
Your request is to configure globally the path to download all files inside ?
Sorry, I don't understand the part "download all files inside".
Current Situation: -> Clicking on a file, it load directly inside the browser.
Wanted Situation: -> Clicking on a file, ask me save or download it.
How:
Enable Attachment mode by taking advantage of "as_attachment
" parameter of send_file
function. Which will add Content-Disposition
header to attachment
in file.
Request: Please Give us an option to "Enable Attachment mode".
So, request is to configure globally the path to add Content-Disposition
header to attachment
for all file inside ?
EDITED
https://github.com/general03/flask-autoindex/blob/424246242c9f40aeb9ac2c8c63f4d2234024256e/flask_autoindex/init.py#L148
https://github.com/general03/flask-autoindex/blob/424246242c9f40aeb9ac2c8c63f4d2234024256e/flask_autoindex/init.py#L150
https://github.com/pallets/flask/blob/7a73171edc3bcffef96ef6367977ab3ae9af9350/src/flask/helpers.py#L499
def send_file(..., as_attachment: bool = False)
I think adding this one argument as_attachment=True
(TRUE) should solve this.
@general03
Hello! any news about this issue? if there any workaround will be appreciated
hey guys i have the same issue i'm using this example: https://github.com/general03/flask-autoindex/issues/16#issuecomment-177625176 so how can i disable this web preview and force download using this way as example?