Skip to content

Instantly share code, notes, and snippets.

@jfontan
Created December 1, 2008 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfontan/30907 to your computer and use it in GitHub Desktop.
Save jfontan/30907 to your computer and use it in GitHub Desktop.
require 'ftools'
FILE_DIR="files"
get "/upload_file/:id" do
@parent=params[:id]
erb :upload_file
end
post "/upload_file/:parent" do
parent_id=params[:parent].to_i
parent=Node.get(parent_id)
file=params["myfile"]
# name got from form data
f_name=file[:filename]
# tmpfile where the file is stored
f_tmp=file[:tempfile]
# this is the directory where I am going to move the file
f_dir=File.join(FILE_DIR, parent_id.to_s)
# directory+filename
filename=File.join(f_dir, f_name)
# actual directory ceration and file movement
Dir.mkdir(f_dir) if !File.exists?(f_dir)
File.move(f_tmp.path, filename)
f_tmp.unlink
node=Node.new
node.name=params[:name]
node.type="file"
node.file=filename
node.node_id=parent_id
node.save
redirect "/show_dir/#{parent_id}"
end
get "/files/*" do
f_path=params["splat"][0]
f_name=f_path.split("/")[-1]
send_file("files/#{f_path}")
#params.inspect
end
__END__
@@ upload_file
<form enctype="multipart/form-data"
action="/upload_file/<%= @parent %>" method="post">
Name: <input type="text" name="name" /> <br />
File: <input name="myfile" type="file" /><br/>
<input type="submit" value="Upload" />
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment