Search code examples
ruby-on-railsrubyimagefile-uploadfilefield

Get full path of file_field image in rails


I am trying to get the full path from an image I am uploading with the rails file_field helper.

%h4 Upload Image
.col-sm-8
  =label_tag "Upload Image"
.col-sm-4  
  =form_tag(controller:"images", :html => {:multipart => true}) do
    =select("app", "app", ["#{Rails.env}_thing", "#{Rails.env}_otherthing"], {include_blank: true})
    =file_field :post, :image
    %button.btn.btn-primary.btn-block{:type => "submit"}
      ="Upload Image"

when it hits the controller action the params are:

{"utf8"=>"✓", "authenticity_token"=>"kVf52OKMdkQPte/jA99R33213123210WRklc2uMhGdM=", "app"=>{"app"=>"development_thing"}, "post"=>{"image"=>"picture.png"}, "html"=>{"multipart"=>"true"}, "action"=>"create", "controller"=>"images"}

What I really want though is the full path of that image file. How would I get that? So if I uploaded from my desktop it would be "/Users/bob/desktop/picture.png"

Thanks!


Solution

  • You can't get the fullpath of file via input type 'file'. Browser will not allow it. Its a security feature in all modern browsers.

    In chrome u will get

    C:\fakepath\filename.ext
    

    while in mozilla u only get filename

    filename.ext
    

    you can find lots of other question related to this. see

    How to get the full path of the file from a file input

    Full path from file input using jQuery