{"title":"File Upload","description":"","section":"cookbook","version":"v1.5","path":"cookbook/file-upload","canonical_url":"https://amberframework.org/docs/v1.5/cookbook/file-upload","markdown_url":"https://amberframework.org/docs/v1.5/cookbook/file-upload.md","inherited":true,"content_markdown":"# File Upload\n\nFile uploading is very simple. Files are detected and turned into `Amber::Router::File` and put into a temporary file cache.\n\nYou can access files from the `params` macro using the `files` method.\n\n```crystal\nparams.files\n```\n\nThis will return a `Hash(String, Amber::Router::File)` object that you can work with. The attributes you can access are:\n\n```crystal\nfile : File\nfilename : String?\nheaders : HTTP::Headers\ncreation_time : Time?\nmodification_time : Time?\nread_time : Time?\nsize : UInt64?\n```\n\nFor 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:\n\n```crystal\n# header: `accept: audio/mp3`\n#\n# POST body\n# {\n#    \"audio_file\": // binary data for the audio file\n#    \"file_name\": \"some_file_name.mp3\"\n# }\ndef create\n  uploaded_file = params.files[\"audio_file\"]\n  uploaded_file_name = params[\"file_name\"]\n  \n  # Do whatever you want with the files and your method 😄\nend\n```\n\n{% hint style=\"warning\" %}\nFirst you need an amber project generated with [Amber CLI](../guides/create-new-app.md) or [from scratch](from-scratch.md).\n{% endhint %}\n\nYou still require a form to upload your file, see [views](../guides/views/). Also see [request and response](../guides/controllers/request-and-response-objects.md)."}