Amber Framework
WebsiteBlogGithubDiscord
  • Introduction
  • Getting Started
  • Guides
    • Overview
    • Installation
    • Create New App
    • Directory Structure
    • Configuration
    • Docker
    • Controllers
      • Sessions
      • Request & Response Objects
      • Halt!
      • Respond With
      • Params
      • Cookies
      • Filters
      • Flash
      • Redirection
      • CSRF
    • Views
      • Basic View Helpers
      • Form Builder
    • Models
      • Granite
        • Granite's README
        • Migrations
        • Validations
        • Callbacks
        • Associations
        • Querying
        • Bulk Insertions
      • Jennifer
        • Jennifer Docs
        • Migrations
        • Models
    • Routing
      • Pipelines
      • Routes
    • Websockets
      • Channels
      • Sockets
      • JavaScript Client
    • Mailers
    • Testing
      • System Tests
  • Deployment
    • Manual Deploy
    • Digital Ocean
    • Heroku
    • Dokku
  • CLI
    • New
    • Recipes
    • Plugins
    • Generate
    • Database
    • Watch
    • Routes
    • Exec
    • Encrypt
  • Examples
    • Amber Auth
    • Crystal Debug
    • Minimal Configuration
  • Cookbook
    • From Scratch
    • Hello World
    • CORS
    • File Download
    • File Upload
    • Cookies
    • Authenticate
    • JSON API
    • JSON Mapping
    • WebSocket Chat
  • Troubleshooting
  • In Production
  • Contributing
  • Code of Conduct
  • HAVE A QUESTION?
    • Join the Discord
    • Follow on Twitter
    • Submit an issue
Powered by GitBook
On this page
  1. Cookbook

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.

params.files

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

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:

# 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
PreviousFile DownloadNextCookies

Last updated 1 year ago

First you need an amber project generated with or .

You still require a form to upload your file, see . Also see .

Amber CLI
from scratch
views
request and response