From 228ff2b8d42b2cf4942e2b952d2c8b824e779c81 Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Thu, 26 Apr 2012 10:56:28 -0400 Subject: [PATCH] Added file uploader Uploaded map files will be saved in /redmine/public/data refs #16 --- app/controllers/maps_controller.rb | 5 +++++ app/models/map.rb | 12 ++++++++++++ app/views/maps/index.html.erb | 14 ++++++-------- config/routes.rb | 1 + 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/app/controllers/maps_controller.rb b/app/controllers/maps_controller.rb index a4027ca..6abece2 100755 --- a/app/controllers/maps_controller.rb +++ b/app/controllers/maps_controller.rb @@ -28,4 +28,9 @@ class MapsController < ApplicationController end + def uploadFile + post = Map.save(params[:upload]) + render :text => "File has been uploaded successfully" + end + end #EOF diff --git a/app/models/map.rb b/app/models/map.rb index fa74131..5efbfd0 100644 --- a/app/models/map.rb +++ b/app/models/map.rb @@ -22,4 +22,16 @@ # if not a new bug issue will be generated. class Map < ActiveRecord::Base unloadable + + require 'digest/sha1' + + def self.save(upload) + name = upload['datafile'].original_filename + directory = "public/data" + sha1 = Digest::SHA1.hexdigest name + # create the file path + path = File.join(directory, sha1) + # write the file + File.open(path, "wb") { |f| f.write(upload['datafile'].read) } + end end diff --git a/app/views/maps/index.html.erb b/app/views/maps/index.html.erb index 6a80a5f..891f533 100755 --- a/app/views/maps/index.html.erb +++ b/app/views/maps/index.html.erb @@ -1,8 +1,7 @@

Exception Handler Maps


- -
+<%= form_tag ({:action => 'uploadFile'}, :multipart => true) %>
@@ -11,10 +10,9 @@

- + <%= form_tag ({:action => 'uploadFile'}, :multipart => true) %> +

:
- -
-
- -

+ <%= file_field 'upload', 'datafile' %>

+ <%= submit_tag "Upload" %> +<%= form_tag %> diff --git a/config/routes.rb b/config/routes.rb index 8b264a9..4a6fe68 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -23,4 +23,5 @@ ActionController::Routing::Routes.draw do |map| map.connect '/exceptionhandler', :controller => 'exceptionhandler', :action => 'index' map.connect '/maps', :controller => 'maps', :action => 'index' + map.connect '/uploadFile', :controller => 'maps', :action => 'uploadFile' end