hug icon indicating copy to clipboard operation
hug copied to clipboard

Change file name returned when using `hug.output_format.file`

Open sameh-sharaf opened this issue 6 years ago • 2 comments

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!

sameh-sharaf avatar May 14 '19 08:05 sameh-sharaf

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}')

sameh-sharaf avatar Jun 13 '19 09:06 sameh-sharaf

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

primary-student avatar Jun 13 '22 03:06 primary-student