-
-
Save wittawasw/9fd4a48dc0c77cf77f8a97ff08235ddc to your computer and use it in GitHub Desktop.
Sinatra File Upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
get "/" do | |
erb :form | |
end | |
post '/save_image' do | |
@filename = params[:file][:filename] | |
file = params[:file][:tempfile] | |
File.open("./public/#{@filename}", 'wb') do |f| | |
f.write(file.read) | |
end | |
erb :show_image | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Image Upload</title> | |
</head> | |
<body> | |
<h1>Upload Image</h1> | |
<form action="/save_image" method="POST" enctype="multipart/form-data"> | |
<input type="file" name="file"> | |
<input type="submit" value="Upload image"> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Show Image</title> | |
</head> | |
<body> | |
<h1>See Image</h1> | |
<img src="/public/<%= @filename %>" /> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment