0

I am using file field control for upload image like below:

<%= form_for(:ImageUpload, :html => { :id => "imageupload" }) do |f| %>
<table width="100%">
    <tr>
        <td align="left">
            <%= file_field( "upload", "datafile") %>
        </td>
    </tr>
</table>
<table width="92%">
            <tr>
               <td align="center">
                  <div class="button" style="margin-right:60px;">
                    <%= f.submit "Next", { :class => "buttonSearch"} %>
                  </div>
               </td>
            </tr>
</table>
<% end %>

controller page:

  def create

    get_img_path = params[:upload][:datafile].path
    @blah = get_img_path

    render 'new'
  end

And I want to get sever path of the uploaded image and also want to change the server path of the image into this app/assets/upload because I want to store the uploaded image at this path app/assets/uploaded. Kindly suggest me, waiting for your reply. Thanks.

user3514596
  • 47
  • 1
  • 6

2 Answers2

1

I'd personally use Paperclip, but if you want a totally "native" solution, why not try this recommendation:

path = params[:file].path

Looking at your updates, it seems you're hitting an issue by only accessing the string element of the path. The solution to that would be to translate it into a File object, which you'll then be able to load the path for

You can see this tutorial on how to go about it:

#app/models/file.rb
Class File < ActiveRecord::Base
 def self.save(upload)
    name =  upload['datafile'].original_filename
    directory = "public/data"
    # create the file path
    path = File.join(directory, name)
    # write the file
    File.open(path, "wb") { |f| f.write(upload['datafile'].read) }
  end
end

This is how you save the file; but if you wanted to get the path, you'd still use the .path method, except you need to do it on a File object

To do this, I looked at several resources, and found this:

file = File.join(Rails.root, 'app', 'csv', 'names.csv')
file.path
Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • I have already seen this link "http://www.tutorialspoint.com/ruby-on-rails/rails-file-uploading.htm" but I have a database app. – user3514596 Apr 10 '14 at 09:29
  • Yep, doesn't the db save the file? How are you saving the file currently? – Richard Peck Apr 10 '14 at 09:30
  • yet i am not saving the file, but my idea is that "I want to get sever path of the uploaded image and change the server path of the image into this app/assets/upload because I want to store the uploaded image at this path app/assets/uploaded" that's why I want to get the server path of the image. – user3514596 Apr 10 '14 at 09:37
  • So you're not saving the file, but you want to store it in the `uploaded` path? That sounds like saving to me? – Richard Peck Apr 10 '14 at 09:47
0

I would recommend you to use Paperclip or Carrierwave gem to handle this.

Paperclip - Click Here

Carrierwave - Click Here

To get the path you can do this

file_path = params[:upload].blank? ? "" : params[:upload].path
Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78