Non-form-based file upload

Hi all,

I have a relatively newbie question regarding the processing of a file
upload from a source other than a Rails form. I’d like to upload a
file using a generic HTTP POST request (which happens, in this case, to
come from a Java applet, though that’s unimportant).

Each upload is a ZIP file that should be associated with particular
activity ID and document ID. The (multipart) form-based version works
fine, using the standard ActionView file_field helper and code like

path = “/files/” + params[:upload][:data].original_filename
root = “#{RAILS_ROOT}/public”
data = params[:upload][:data].read
File.open(path, ‘wb’) do |f|
f.write(data)
end

…in the controller.

What I’m wondering is how to handle the applet-based upload. I
figured it might help to have a custom route defined that the applet
would use, something like:

map.connect ‘activities/:activity_id/:document_id’,
:controller => ‘activities’, :action => ‘new_upload_applet’,
:activity_id => nil, :document_id => nil,
:requirements => {:activity_id => /\d/, :document_id => /\d/}

But then what? In the (hypothetical) new_upload_applet action, how do
I access the file data itself (stored under params[:upload][:data] in
the form-based version)?

Any help would be greatly appreciated. Thanks in advance,

Garrett

Garrett Weinberg wrote:

What I’m wondering is how to handle the applet-based upload. I
figured it might help to have a custom route defined that the applet
would use, something like:

map.connect ‘activities/:activity_id/:document_id’,
:controller => ‘activities’, :action => ‘new_upload_applet’,
:activity_id => nil, :document_id => nil,
:requirements => {:activity_id => /\d/, :document_id => /\d/}

But then what? In the (hypothetical) new_upload_applet action, how do
I access the file data itself (stored under params[:upload][:data] in
the form-based version)?

Any help would be greatly appreciated. Thanks in advance,

Garrett

In theory, the applet should form a POST request that is
indistinguishable from one submitted by the browser. Or if you are on
edge, you can post a restful xml query and rails will extract the
params. Although I am not sure how to encode binary data in the xml
request. Base64 perhaps?

And if your not edge, you can still POST xml with a Base64 encoded file
and just manually parse it with REXML standard library.

uhm … a form-base upload is only a HTTP POST Request, isnt it?.

so it should not make a difference weither you send that POST via an
applet or form. Rails only sees a POST Request coming in, it cant see
if it was eend by a applet,by an ajax call or a form or whatever…

in all cases, the file should be acceccible through the params hash.

dont see where the problem is. or am i missing something?

Thorsten L wrote:

uhm … a form-base upload is only a HTTP POST Request, isnt it?.

so it should not make a difference weither you send that POST via an
applet or form. Rails only sees a POST Request coming in, it cant see
if it was eend by a applet,by an ajax call or a form or whatever…

in all cases, the file should be acceccible through the params hash.

dont see where the problem is. or am i missing something?

Yes, it seems you understand the situation. My (newbie) question is
where in the params hash to look for the file given a particular
custom route that the applet uses for the HTTP POST. So a basic example
route & the params hash this would generate would help me out a lot.
The params hash is more obvious to me in the normal, form-based version
because I define the ActionView’s RHTML myself. It’s the non
form-based version that has me confused.

Thanks again,
Garrett

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs