Python 2.7 如何使用Web2py自定义下载功能
我正在web2py中开发一个简单的webapp,我想创建一个链接,让用户下载一个文件。像这样:Python 2.7 如何使用Web2py自定义下载功能,python-2.7,asynchronous,download,web2py,Python 2.7,Asynchronous,Download,Web2py,我正在web2py中开发一个简单的webapp,我想创建一个链接,让用户下载一个文件。像这样: <a href="{{=URL('download',args = FILE)}}" download> <a href="{{=URL('custom_download',args = FILEID)}}" download> def custom_download(): download_row = db(db.computers.FILEID == requ
<a href="{{=URL('download',args = FILE)}}" download>
<a href="{{=URL('custom_download',args = FILEID)}}" download>
def custom_download():
download_row = db(db.computers.FILEID == request.args(0)).select()
download_file = download_row.filefield
return download_file
但是,我不完全确定我需要写什么才能让它工作。我假设您的文件存储在
上传
文件夹中,那么您的自定义下载功能将是:
def custom_download():
download_row = db(db.computers.FILEID == request.args(0)).select().first()
download_file = download_row.filefield
# Name of file is table_name.field.XXXXX.ext, so retrieve original file name
org_file_name = db.computers.filefield.retrieve(download_file)[0]
file_header = "attachment; filename=" + org_file_name
response.headers['ContentType'] = "application/octet-stream"
response.headers['Content-Disposition'] = file_header
file_full_path = os.path.join(request.folder, 'uploads', download_file)
fh = open(file_full_path, 'rb')
return response.stream(fh)