10  Files

File uploads (attachments to individual records) are a unique field type in REDCap that accept a variety of file types, including images, pdfs, and many more. Unlike other export methods, exporting files only works for one file field from one record at a time.

If the project has repeating events (i.e. a longitudinal project), the event name that the record is in must be specified. If the file of interest is in a repeat instance, the instance number must also be specified.

In this example we will download the test file from record 1 in the test_upload field in case_intake_arm_1 event, and save the file to a specified location.

redcap_file_download_oneshot()

%%capture
%%R
redcap_file_download_oneshot(
    event = "case_intake_arm_1",
    directory = "./files/export_files/",
    file_name = "test_file_export_r.png",
    record = 1,
    field = "test_upload",
    redcap_uri = url,
    token = token,
    overwrite = TRUE
)

export_file()

In python we will use the IPython.diplay module to view the downloaded file.

export_file_image = project.export_file(record="1", 
                                        field="test_upload", 
                                        event="case_intake_arm_1")
with open("files/export_files/test_file_export_py.png","wb") as binary_file:
    binary_file.write(export_file_image[0])
from IPython.display import Image
Image("files/export_files/test_file_export_py.png", width=300)