views:

110

answers:

1

I am trying use a Java Uploader in a ROR app (for its ease of uploading entire directories). The selected uploader comes with some PHP code that saves the files to the server. I am trying to translate this code to Ruby, but am stumped on this point:

PHP has a very convenient superglobal – $_FILES – that contains a hash of all files uploaded to the current script via the HTTP POST method. It appears Ruby does not have a similar resource. Lacking that, what is the best way to access and save the uploaded files?

I am using the JavaPowUpload uploader ( http://www.element-it.com/OnlineHelpJavaPowUpload/index.html ).

A: 

ruby on rails allows you use the application root directory to get at the file stored (wherever you have decided to put it) via #{RAILS_ROOT}.

Check out this tutorial. Not the prettiest method, but it should give you an idea of what needs to be done. Once the file is uploaded, it's just a matter of getting the right path and doing your processing from there.

dhoss
I've actually been through that tutorial, and created upload forms from scratch using custom multipart forms. The challenge I have here is that the Java uploader create's its own POST event, so I don't have access to the ROR params. So I think I need a straight Ruby solution.
kingjeffrey
so, the java bit is POSTing to your app, right? So you should still have read access to the appropriate POST params from rails. You just need to make sure you know the names of the parameters coming from the java bit. That, or you could, as you said, write the whole upload portion in the rails app.
dhoss
@Jeff, even if the Java app creates its own `POST` you still have access to the multipart data as files, you just have to figure out where in the `params` hash they ended up. Call `logger.warn(params.inspect)` from your controller and then look at the log, take it from there.
vladr
yea, Vlad nailed it in a more succinct wording. +1
dhoss
Thank you Vlad and dhoss. I am further down the road, but haven't quite figured it all out yet. I am seeing the params hash, but input fields appear as temp files and the uploaded file is missing. I also need to figure out authentication. The authentication_token is not validating. I'll bang my head against this wall for another day or two, and hopefully figure this all out.Thank you again for pointing me in the right direction.I really enjoy Ruby, but sometimes miss the documentation and robustness of PHP: http://www.php.net/manual/en/features.file-upload.post-method.php
kingjeffrey