hug
hug copied to clipboard
Change file name returned when using `hug.output_format.file`
Hi,
I have an API to return a CSV file with table contents with output=hug.output_format.file which works perfectly. However, I notice the file name is random.
Is there a way to name the file returned by Hug? Thanks!
Here's how you set the attachment file name.
Use set_header() to set response headers:
response.set_header('Content-Disposition', 'attachment; filename={your-file-name.ext}')
Hi, I want to create an API to download files, but I don't know how to set the filename when downloaded by the browser.
@hug.get("/file", output = hug.output_format.file)
def download_file(file_path):
file_name = os.path.basename(file_path)
response.set_header('Content-Disposition', f'attachment; filename={file_name}')
return os.path.abspath(file_path)
This prompts me with an error "NameError: name 'response' is not defined". @sameh-sharaf