Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 如何使用Web2py自定义下载功能_Python 2.7_Asynchronous_Download_Web2py - Fatal编程技术网

Python 2.7 如何使用Web2py自定义下载功能

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

我正在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 == 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)