Suppose we have employee table with resume as uplaod field. And we want to download file on the basis of employee id.
Download url is like
app_name/custom_download/employee_id
or
<a href="{{=URL('default', 'custom_download', args = employee_id)}}" download>
example: www.example.com/default/custom_download/1
Consider following employee schema:
1 2 3 4 5 |
db.define_table('employee', Field('name', 'string', required=True), Field('email', string, required=True), Field('address', 'text', required=True), Field('resume', 'upload', required=True)) |
Then custom download controller will be:
1 2 3 4 5 6 7 8 9 10 11 12 |
def custom_download(): employee_id = request.args(0) emp_record = db(db.employee.id == employee_id).select().first() download_file = emp_record.resume # Name of file is table_name.field.XXXXX.ext, so retrieve original file name org_file_name, file_stream = db.employee.resume.retrieve(download_file) file_header = "attachment; filename=" + org_file_name response.headers['ContentType'] = "application/octet-stream" response.headers['Content-Disposition'] = file_header return response.stream(file_stream) |
If you have any queries related to this code, please comment below.
Recent Comments