# Non-form-based file upload

**URL:** <https://www.ruby-forum.com/t/non-form-based-file-upload/71945>\
**Category:** Rails\
**Created:** [October 12, 2006, 6:05pm UTC](https://www.ruby-forum.com/t/non-form-based-file-upload/71945 "2006-10-12T18:05:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![steely1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@steely1](https://www.ruby-forum.com/u/steely1)\
**Post date:** [October 12, 2006, 6:05pm UTC](https://www.ruby-forum.com/t/non-form-based-file-upload/71945/1 "2006-10-12T18:05:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![steely1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@steely1](https://www.ruby-forum.com/u/steely1)\
**Post date:** [October 12, 2006, 6:30pm UTC](https://www.ruby-forum.com/t/non-form-based-file-upload/71945/2 "2006-10-12T18:30:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![steely1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@steely1](https://www.ruby-forum.com/u/steely1)\
**Post date:** [October 18, 2006, 10:38am UTC](https://www.ruby-forum.com/t/non-form-based-file-upload/71945/3 "2006-10-18T10:38:51Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![steely1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@steely1](https://www.ruby-forum.com/u/steely1)\
**Post date:** [October 18, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/non-form-based-file-upload/71945/4 "2006-10-18T14:16:52Z")

</div>

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
