How can I get the full path of my selected file in Ruby on Rails?
I have put a file_field
in my view :
<% form_for(:uploaded_file, @feed, :url => {:action=>'copy'}, :html=> {:multipart=>true}) do |f| %>
<%= f.file_field :uploaded_file %> <br>
<%= f.submit "Submit" %>
<% end %>
And I have a method in my controller that reads a Excel file:
def copy
file = ""
book = Spreadsheet.open 'excel_file'
table = book.worksheet 'excel_sheet'
table.each do |row|
file << row
end
end
Log with request
"authenticity_token"=>"BbQRomTLhiF2O54/G6eHwnnaWbLttUSvo31FO3ZtKoA=",
"uploaded_file"=>{"uploaded_file"=>#<ActionDispatch::Http::UploadedFile:
0x27b7ab0 @original_filename="test.xlsx",@content_type="application
/vnd.openxmlformats-offi开发者_StackOverflow社区cedocument.spreadsheetml.sheet", @headers=
"Content-Disposition: form-data; name=\"uploaded_file[uploaded_file]\";
filename=\"volumetrie.xlsx\"\r\nContent-Type: application/vnd.openxmlformats
officedocument.spreadsheetml.sheet\r\n", @tempfile=#<File:C:/DOCUME~1/me/
LOCALS~1/Temp/RackMultipart20110714-524-fy6vu>>}, "commit"=>"Submit"}
I'm just trying to get the path of my selected file to insert it in my Spreadsheet.open method. The error message is : No such file or directory.
Instead of 'excel_file'
that is the path of my Excel file, I would like to use the path of the file that I have choosed in the file_field
in my view. How can I use this parameter ?
Thank you !
File.absolute_path(file)
Ruby Doc
You can't get path to file from form in modern browsers. You can work only with tempfile Should be available as params[ :uploaded_file ][ :tempfile ]
Edit try
file = Spreadshet::Excel.new('params[ :uploaded_file ][ :filename ]', 'w+')
file.write( params[ :uploaded_file ][ :tempfile ].read )
精彩评论