2

In my Rails controller I have the line

send_file("#{Rails.root}/test/image.png")

I'd like my web page to have a textbox, that the user can fill in a directory such as C:/OldPictures/All/, and then when the user clicks a link which invokes the controller, it also reads the directory in the textbox, and downloads the file image.png to the specified directory. Can I make it so?

JJ Beck
  • 5,193
  • 7
  • 32
  • 36
  • My instinct is to say that the server (Rails or otherwise) doesn't and cannot specify where a file is downloaded to - that for security reasons, the user/browser/OS has to make that decision, and all the server can do is say "here's the file, do whatever it is you do with it". – MrTheWalrus Nov 04 '12 at 21:39

2 Answers2

4

No.

For security reasons, web servers (and web code generally, including JavaScript) cannot see the client's filesystem or specify where files are downloaded to. You can suggest a filename for the download, but that's it (use the :filename option to send_file if you want to set this to something other than the name the file has on the server). The download location is determined by some combination of the browser, the OS, and the user.

Community
  • 1
  • 1
MrTheWalrus
  • 9,670
  • 2
  • 42
  • 66
1

Short answer, yes

Long answer, we don't usually allow client side apps to have knowledge about file locations on the server for various reasons. Users should be 'decoupled' from where files are actually physically stored when dealing with web applications

Now for the code:

<form action="your_action_url" method="get" >
  <input name="location" type="text" />
  ...
</form>

Meanwhile, in your controller

send_file(File.join(params[:location], "image.png"))
ilan berci
  • 3,883
  • 1
  • 16
  • 21