Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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>
Add Comment
Please, Sign In to add comment