Skip to main content

Upload File

Uploads a new file to the system. The file will be associated with the authenticated user.

HTTP Request

POST/api/media/files

Authorization

Authorization

  • Required: Yes
  • Permission: Staff or Admin
  • Authentication: Token-based (Authorization: Token <your_api_key>)

Request Body

FieldTypeRequiredDescription
fFileYesFile to upload (multipart/form-data)
nameStringNoCustom filename (defaults to original filename)
descriptionStringNoFile description

Example Requests

1import requests
2
3# Upload a file
4with open('image.jpg', 'rb') as file:
5  response = requests.post('http://www.example.com/api/media/files', 
6      files={
7          'f': ('image.jpg', file, 'image/jpeg')
8      },
9      data={
10          'name': 'Profile Picture',
11          'description': 'User profile avatar'
12      },
13      headers={'Authorization': 'Token <your_api_key>'}
14  )
15print(response.json())
16
17# Upload with minimal data
18with open('document.pdf', 'rb') as file:
19  response = requests.post('http://www.example.com/api/media/files', 
20      files={
21          'f': ('document.pdf', file, 'application/pdf')
22      },
23      headers={'Authorization': 'Token <your_api_key>'}
24  )
25print(response.json())

Status Codes

CodeDescription
201File uploaded successfully
400Bad request — invalid file or input
401Unauthorized — authentication required
403Forbidden — insufficient permissions
413File too large
415Unsupported media type
500Internal server error

Response Fields

FieldTypeDescription
idIntegerUnique ID of the file
typeStringFile type (read-only, auto-detected)
userObjectUser who uploaded the file with id, username, full_name
fStringFile URL/path
nameStringOriginal filename
descriptionStringFile description
sizeIntegerFile size in bytes (read-only)
human_readable_sizeStringHuman-readable file size (e.g., "2.5 MB")
widthIntegerImage width in pixels (images only, read-only)
heightIntegerImage height in pixels (images only, read-only)
modeStringImage color mode (images only, read-only)
created_atString (ISO 8601)Timestamp when file was uploaded
updated_atString (ISO 8601)Timestamp when file was last updated

Notes

    • File upload requires multipart/form-data content type
    • File type is automatically detected based on content and MIME type
    • For image files, dimensions and color mode are automatically extracted
    • Files are associated with the authenticated user who uploads them
    • File size is automatically calculated and stored in both bytes and human-readable format
    • Only the 'f' field is required; name and description are optional
    • If name is not provided, the original filename will be used