22  Files

Files are optional attachments to individual records.

File uploads are a unique field type in REDCap that accept a variety of file types, including images and other documents. Unlike other export methods, importing files only works for one file field for 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 field of interest is in a repeat instance, the instance number must also be specified.

redcap_file_upload_oneshot()

%%capture --no-stdout 
%%R
redcap_file_upload_oneshot(file_name='./files/test_file.png', record=7, field='test_upload', event='case_intake_arm_1', redcap_uri=url, token=token)
$success
[1] TRUE

$status_code
[1] 200

$outcome_message
[1] "file uploaded to REDCap in 1.0 seconds."

$records_affected_count
[1] 1

$affected_ids
[1] "7"

$elapsed_seconds
[1] 1.024144

$raw_text
[1] ""

import_file()

tmp_file = tempfile.TemporaryFile()
project.import_file(record="7",
                 field="test_upload", 
                 file_name="./files/test_file.png",
                 file_object=tmp_file,
                 event="case_intake_arm_1")
[{}]

The output is a list of an empty JSON object, as expected for a successful file import using this method.