Last active
August 29, 2015 14:14
-
-
Save TheAnonymous/2e90ca83950ab1fc5893 to your computer and use it in GitHub Desktop.
This is a very simple sinatra webserver to upload/download files. You should create a folder with the name "public" thats where the uploaded files are going. Also please make a folder with the name "views" and place the form.erb there. Thats all have fun :)
This file contains hidden or 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>File Upload</title> | |
</head> | |
<body> | |
<h1>Upload File</h1> | |
<form action="/save_file" method="POST" enctype="multipart/form-data"> | |
<input type="file" name="file"> | |
<input type="submit" value="Upload"> | |
</form> | |
<% for @file in @files %> | |
<li><a href="<%=@file%>"> <%=@file%> </a></li> | |
<% end %> | |
</body> | |
</html> |
This file contains hidden or 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' | |
set :environment, :production | |
set :port, 80 | |
get "/" do | |
@files = Dir["./public/*"] | |
erb :form | |
end | |
post '/save_file' do | |
@filename = params[:file][:filename] | |
file = params[:file][:tempfile] | |
if File.exists? "./public/#{@filename}" then | |
"File with this name exists already!" | |
else | |
File.open("./public/#{@filename}", 'wb') do |f| | |
f.write(file.read) | |
end | |
"File uploaded" | |
end | |
end | |
get "/public/:file" do | |
send_file File.open("./public/#{params[:file]}") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment