Documentation

File Upload

Browse documentation

Read this page as HTML, Markdown, or structured JSON—or open the published Markdown with an AI assistant. Gemini receives the prompt through your clipboard because its signed-out page does not reliably prefill URL text; paste when the new tab opens. External assistants need the public site URL.

File Upload

File uploading is very simple. Files are detected and turned into Amber::Router::File and put into a temporary file cache.

You can access files from the params macro using the files method.

Crystal
params.files

This will return a Hash(String, Amber::Router::File) object that you can work with. The attributes you can access are:

Crystal
file : File
filename : String?
headers : HTTP::Headers
creation_time : Time?
modification_time : Time?
read_time : Time?
size : UInt64?

For example, let's say we have a controller with a create method that we want someone to POST a JSON body that includes a file to upload:

Crystal
# header: `accept: audio/mp3`
#
# POST body
# {
#    "audio_file": // binary data for the audio file
#    "file_name": "some_file_name.mp3"
# }
def create
  uploaded_file = params.files["audio_file"]
  uploaded_file_name = params["file_name"]
  
  # Do whatever you want with the files and your method 😄
end

First you need an amber project generated with Amber CLI or from scratch.

You still require a form to upload your file, see views. Also see request and response.