Skip to content

Script Development / Returning Files DFF.RESP_FILE

When the returned content is a "file on disk", you can use DFF.RESP_FILE(...) for detailed control.

Parameter Type Required / Default Description
file_path str Required Specifies the path of the file to return (relative to the resource file directory).
status_code int 200 Same as the parameter of the same name in DFF.RESP(...).
headers dict None Same as the parameter of the same name in DFF.RESP(...).
allow_304 bool False Same as the parameter of the same name in DFF.RESP(...).
auto_delete bool False When set to True, the file is automatically deleted from the disk after download.
download bool / str True By default, downloads the file with the same name as the original file.
When set to False, instructs the browser to open the file directly if possible.
When a string is specified, uses the specified value as the download filename.

DFF.RESP_FILE(...) automatically fills the HTTP Content-Type header based on the file extension. By default, it uses the file_path. When a download string is specified, it uses the download value as the filename for download.

The 'Resource File Directory' refers to the /data/resources folder inside the container. After normal deployment, this folder is mounted to the host machine's disk for persistent storage.

Example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
@DFF.API('Example 1')
def case_1():
    '''
    Download the user-guide.pdf file from the resource file directory.
    '''
    return DFF.RESP_FILE('user-guide.pdf')

@DFF.API('Example 2')
def case_2():
    '''
    Instruct the browser to open the `user-guide.pdf` file from the resource directory online.
    '''
    return DFF.RESP_FILE('user-guide.pdf', download=False)

@DFF.API('Example 3')
def case_3():
    '''
    Open the index.html page from the resource directory in the browser.
    '''
    return DFF.RESP_FILE('index.html', download=False)

@DFF.API('Example 4')
def case_4():
    '''
    Download the servey.xlsx file from the resource file directory and save it as "Survey.xlsx".
    '''
    return DFF.RESP_FILE('servey.xlsx', download='Survey.xlsx')

@DFF.API('Example 5')
def case_5():
    '''
    Specify additional response headers.
    '''
    headers = {
        'X-Author': 'Tom',
    }
    return DFF.RESP_FILE('user-guide.pdf', headers=headers)