Reputation: 12926
Is it possible to have a crystal based web server which handles file upload? I have been looking in the docs, and in many of the crystal web frameworks. I have not found any reference to a simple file upload feature anywhere.
Is this possible, or do I have to look elsewhere to handle my image-uploads?
Upvotes: 1
Views: 932
Reputation: 2376
Here is how you can upload file in shivneri framework
class FileController < Controller
@[Worker("POST")]
@[Route("/upload")]
def upload_file
path_to_save = File.join(Dir.current, "upload/upload.png")
field = "fort"
if (file.is_exist(field))
file.save_to(field, path_to_save)
return json_result({
message: "file saved"
})
else
result = {
message: "file not saved",
}
return json_result(result)
end
end
end
For more info, visit shivneri doc - https://shivneriforcrystal.com/tutorial/file/
Upvotes: 0
Reputation: 5157
UPDATED ANSWER: As of November 2016 you can use Kemal v0.16.1 and Crystal 0.19.4 for File uploads.
Here's how you can use it
post "/upload" do |env|
parse_multipart(env) do |f|
image1 = f.data if f.field == "image1"
image2 = f.data if f.field == "image2"
puts f.meta
puts f.headers
"Upload complete"
end
end
Upvotes: 1
Reputation: 629
There is no support for multipart/form-data (https://www.rfc-editor.org/rfc/rfc1867) in crystal right now. It is something that should arrive in the std IMO.
But right now, Serdar Dogruyol seems to have invested some of his time around that:
Upvotes: 2