1

I have a field on my model Site called file_link. In the edit form, I want there to be a field for file_link with a Browse button next to the field, which pulls up a file browser on their local computer. I want them to be able to select a file, then have rails save the users local Path to the file, not the actual file.

For Example: file_link should save the path: N:\Projects\excelfile.xlsx

How can this be achieved?

MoB
  • 448
  • 1
  • 4
  • 15
  • Just wondering why would you want to save the local file path without the file? – Sharjeel Apr 05 '16 at 22:43
  • @Sharj Our app is an internal tool, and we want to have a field that acts as a link to one of our internal network drives. – MoB Apr 06 '16 at 17:06

2 Answers2

0

when you visit a website, there is no way for the website to access your filesystem unless they somehow hack you. This isn't specific to Rails sites ... it's a fundemental security precaution of the internet. If you want to access your users' filesystem, you may be able able to do it with Javascript after gettin their permission, but might not be be easy. See Can javascript access a filesystem?

However, if you are building this app for localhost use only, you can use Ruby to manipulate/show the filesystem all you won't. But it's going to be limited to the filesystem running the Ruby program.

Community
  • 1
  • 1
max pleaner
  • 26,189
  • 9
  • 66
  • 118
0

There few gems able to help you with that. And one of them is called Carriervawe.

Gem LINK

u = Site.new
u.file_link = params[:file] # Assign a file like this, or

# like this
File.open('somewhere') do |f|
  u.file_link = f
end

u.save!
u.file_link.url # => '/url/to/file.png'
u.file_link.current_path # => 'path/to/file.png'
u.file_link_identifier # => 'file.png'
7urkm3n
  • 6,054
  • 4
  • 29
  • 46
  • Wouldn't this save the file path on the Server rather than the file path on the client's computer? – MoB Apr 06 '16 at 17:11
  • 1
    in `carriervawe` gem u can set where do u want to save it too. Once passing file, it saves image to where ever u set and store `url` in db. – 7urkm3n Apr 06 '16 at 17:46
  • @7urk3m3n thank you, but I feel you are misconceiving my question. I don't want to upload the file. The application and its server would not be able to access the file. The file simply already exists on a network drive. All I want to save is a path, generated by the client, using a file browser. Not a path generated by the application/server. So If I used it, I would save 'C:\Intel\file.xlsx\' to the database, a file that is not accessible by the web-server at all. – MoB Apr 06 '16 at 17:56