Simplify file upload parsing and support multiple uploads with same name (#458)

This commit is contained in:
Serdar Dogruyol 2018-06-27 23:33:28 +03:00 committed by GitHub
parent f916bad095
commit eed97877a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 2 deletions

View File

@ -1,3 +1,23 @@
# Next
- `env.params.files` is now an `Array(FileUpload)`. You can iterate over to access the images.
```ruby
env.params.files.each do |file|
filename = file.filename
if !filename.is_a?(String)
"No filename included in upload"
else
file_path = ::File.join [Kemal.config.public_folder, "uploads/", filename]
File.open(file_path, "w") do |f|
IO.copy(file.tmpfile, f)
end
"Upload OK"
end
```
# 0.23.0 (17-06-2018)
- Crystal 0.25.0 support 🎉

View File

@ -16,7 +16,7 @@ module Kemal
@query = HTTP::Params.new({} of String => Array(String))
@body = HTTP::Params.new({} of String => Array(String))
@json = {} of String => AllParamTypes
@files = {} of String => FileUpload
@files = [] of FileUpload
@url_parsed = false
@query_parsed = false
@body_parsed = false
@ -71,7 +71,7 @@ module Kemal
next unless upload
filename = upload.filename
if !filename.nil?
@files[upload.name] = FileUpload.new(upload: upload)
@files << FileUpload.new(upload: upload)
else
@body.add(upload.name, upload.body.gets_to_end)
end