[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to send a file from the http client to the server?

Abir B.

10/23/2007 9:01:00 AM

Hello
I've a http server built with ruby based on Mongrel, it must receive a
file from the client http (in the request coming from a web browser or
in request sent by a httpClient written in ruby)
How can I send the file from the http client to the server?
Thanks
--
Posted via http://www.ruby-....

11 Answers

Robert Dober

10/23/2007 9:21:00 AM

0

On 10/23/07, Abir B. <abboura84@yahoo.fr> wrote:
> Hello
> I've a http server built with ruby based on Mongrel, it must receive a
> file from the client http (in the request coming from a web browser or
> in request sent by a httpClient written in ruby)
> How can I send the file from the http client to the server?
> Thanks
> --
> Posted via http://www.ruby-....
>
>

Hmm the following might not work out of the box, but I copied the essential
stuff from my file server:

HTH
Robert
########################################################################
class UploadServlet < WEBrick::HTTPServlet::AbstractServlet
def upload_form res
styles =""
res.body = <<-EOS
<html>
<head>
<title>WEBrick File Upload Service</title>
#{styles}
</head>
<div class="headline">
WEBrick File Upload Service
</div>
<div class="input_form">
<form method="POST" enctype="multipart/form-data">
<input type="file" name="data" size="100" />
<div class="center leave_sp_ab1">
<input type="submit" value="Upload File" />
</div>
</form>
</div>
</html>
EOS

def do_GET(req, res)
res["content-type"] = "text/html"
upload_form res
end

def do_POST(req, res)
res['content-type']="text/html"

length = req['content-length'].to_i
#<snip>

upload_data = req.query["data"]
filename = upload_data.filename
File.open("whatever", "wb") do |file|
upload_data.each_data do |data|
file << data.to_s
end
end
end
end # class UploadServlet

svr =
WEBrick::HTTPServer.new(
:Port => 4242,
:ListenAddress => "1.2.3.4")

svr.mount("/upload", UploadServlet,)

trap(:INT){ svr.shutdown }
svr.start
--------------------- 8< --------------------
--
what do I think about Ruby?
http://ruby-smalltalk.blo...

Abir B.

10/23/2007 10:06:00 AM

0

Thanks for response
But what I need to do is to build the request. I must know how to code
the request or the Client.
--
Posted via http://www.ruby-....

Robert Dober

10/23/2007 10:31:00 AM

0

On 10/23/07, Abir B. <abboura84@yahoo.fr> wrote:
> Thanks for response
> But what I need to do is to build the request. I must know how to code
> the request or the Client.
> --
> Posted via http://www.ruby-....
>
>

Well actually it is difficult to give the correct response if one does
not read the question :(
Sorry for the noise. Unfortunately I do not know how to encode the
multipart file, but hopefully somebody does, Pit?

Cheers
Robert

Abir B.

10/23/2007 12:43:00 PM

0

Hi
I created a form wich allows to select a file and send it to the server
:
-----------------
class FormHandler < Mongrel::HttpHandler
def process(req, resp)
resp.start do |head, body|
body << "<html>"
body << "<body>"
body << "<form name=""formulaire_envoi_fichier""
enctype=""multipart/form-data"" method=""post"" action=""./test"">"
body << "<input type=""file"" name=""fichier_choisi"">"
body << "<br>"
body << " <br>"
body << " <input type=""submit"" name=""bouton_submit""
value=""Envoyer le fichier"">"
body << "</body>"
body << "</html>"
end
end
end

class MyHandler < Mongrel::HttpHandler
attr_accessor :responses_file
def initialize
end
def process(req, resp)
resp.start do |head, body|
puts "Req.inspect",req.inspect
puts "Req params",req.params

i=0

req.body.each_line do |l|
puts l
end

puts
print i," lignes"

body.write("<html>");
body.write(" <head>");
body.write(" <title>Test Page</title>");
body.write(" </head>");
body.write(" <body>");
...
body << "</body></html>"
end
end
end
h = Mongrel::HttpServer.new("0.0.0.0", "3000")
h.register("/test", MyHandler.new)
h.register("/acc",FormHandler.new)
h.run.join

end


---------------

Then when I run in the test page http://localhost:3000/test, I can see
the content of the file in the request's body (puts l). But I don't know
how to extract this data. I've to work on the string?
Another question, how can I send the file directly (without passing by
the form).
Thanks

--
Posted via http://www.ruby-....

Robert Dober

10/23/2007 1:57:00 PM

0

On 10/23/07, Abir B. <abboura84@yahoo.fr> wrote:
> Hi
> I created a form wich allows to select a file and send it to the server
> :
> -----------------
> class FormHandler < Mongrel::HttpHandler
> def process(req, resp)
> resp.start do |head, body|
> body << "<html>"
> body << "<body>"
> body << "<form name=""formulaire_envoi_fichier""
> enctype=""multipart/form-data"" method=""post"" action=""./test"">"
> body << "<input type=""file"" name=""fichier_choisi"">"
> body << "<br>"
> body << " <br>"
> body << " <input type=""submit"" name=""bouton_submit""
> value=""Envoyer le fichier"">"
> body << "</body>"
> body << "</html>"
> end
> end
> end
>
> class MyHandler < Mongrel::HttpHandler
> attr_accessor :responses_file
> def initialize
> end
> def process(req, resp)
> resp.start do |head, body|
> puts "Req.inspect",req.inspect
> puts "Req params",req.params
>
> i=0
>
> req.body.each_line do |l|
> puts l
> end
>
> puts
> print i," lignes"
>
> body.write("<html>");
> body.write(" <head>");
> body.write(" <title>Test Page</title>");
> body.write(" </head>");
> body.write(" <body>");
> ...
> body << "</body></html>"
> end
> end
> end
> h = Mongrel::HttpServer.new("0.0.0.0", "3000")
> h.register("/test", MyHandler.new)
> h.register("/acc",FormHandler.new)
> h.run.join
>
> end
>
>
> ---------------
>
> Then when I run in the test page http://localhost:3000/test, I can see
> the content of the file in the request's body (puts l). But I don't know
> how to extract this data.

Now I am confused, look into the method do_POST of my first post and
you will see how to extract this data.
R.
--
what do I think about Ruby?
http://ruby-smalltalk.blo...

Richard Conroy

10/23/2007 2:06:00 PM

0

Abir,
are you using a HTTP client library? Or are you using something
more basic like
raw net/http or open-uri ? You could cheat and use something a bit
more high level
like Hpricot.

Or are you using something non-Ruby like curl, and need to know exactly how you
pad out the HTTP Header?

Abir B.

10/23/2007 2:37:00 PM

0

Richard Conroy wrote:
> Abir,
> are you using a HTTP client library? Or are you using something
> more basic like
> raw net/http or open-uri ? You could cheat and use something a bit
> more high level
> like Hpricot.
>
> Or are you using something non-Ruby like curl, and need to know exactly
> how you
> pad out the HTTP Header?

I used Ruby (Mongrel) to code the server, and for the client I must use
ruby because the file will be generated by the client and then sent to
the server, but I'm confused between httpclient, net/http, open-uri ...
I don't know the best choice wich respond to my needs.

P.S : Robert Dober I can't run your example because httprequest in
Mongrel is different from webrick (has only 2 attributes body and
params).

thanks

--
Posted via http://www.ruby-....

Robert Dober

10/23/2007 2:48:00 PM

0

On 10/23/07, Abir B. <abboura84@yahoo.fr> wrote:
> Richard Conroy wrote:
> > Abir,
> > are you using a HTTP client library? Or are you using something
> > more basic like
> > raw net/http or open-uri ? You could cheat and use something a bit
> > more high level
> > like Hpricot.
> >
> > Or are you using something non-Ruby like curl, and need to know exactly
> > how you
> > pad out the HTTP Header?
>
> I used Ruby (Mongrel) to code the server, and for the client I must use
> ruby because the file will be generated by the client and then sent to
> the server, but I'm confused between httpclient, net/http, open-uri ...
> I don't know the best choice wich respond to my needs.
>
> P.S : Robert Dober I can't run your example because httprequest in
> Mongrel is different from webrick (has only 2 attributes body and
> params).
strange I'd rather thought that the req object in your upload should
be the same?

Richard Conroy

10/23/2007 4:30:00 PM

0

On 10/23/07, Abir B. <abboura84@yahoo.fr> wrote:
> Richard Conroy wrote:
> > Abir,
> > Or are you using something non-Ruby like curl, and need to know exactly
> > how you
> > pad out the HTTP Header?
>
> I used Ruby (Mongrel) to code the server, and for the client I must use
> ruby because the file will be generated by the client and then sent to
> the server, but I'm confused between httpclient, net/http, open-uri ...
> I don't know the best choice wich respond to my needs.

I feel your pain. I don't know httpclient directly, but net/http is very
low level and difficult to work with when you just want to get stuff
done, or validate a server design. Also open-uri, which is super-friendly
to use, can't use POST, which is a bit of a deal breaker.

AFAIK there isn't a solid contender in the Ruby Http Library space.

Also, you are sending up a file. As far as your Http Client is concerned,
it can ignore the 'file' input from the form.

You might want to take a look at some alternative HttpClients (e.g. Hpricot,
WATIR, WWW::Mechanize, or look at some open source application
specific http clients (like the 2 clients that are used to connect
Ruby with Amazon's S3 service).

Alternatively the O'Reilly RESTful Web Services book, has some advice
on this, and the authors made some modifications to open-uri to support
all the HTTP verbs: just gem install rest-open-uri, and you might get
open-uri friendliness without the deal breakers.

Phil Meier

10/24/2007 6:14:00 PM

0

Abir B. schrieb:
> Richard Conroy wrote:
>> Abir,
>> are you using a HTTP client library? Or are you using something
>> more basic like
>> raw net/http or open-uri ? You could cheat and use something a bit
>> more high level
>> like Hpricot.
>>
>> Or are you using something non-Ruby like curl, and need to know exactly
>> how you
>> pad out the HTTP Header?
>
> I used Ruby (Mongrel) to code the server, and for the client I must use
> ruby because the file will be generated by the client and then sent to
> the server, but I'm confused between httpclient, net/http, open-uri ...
> I don't know the best choice wich respond to my needs.
>
> P.S : Robert Dober I can't run your example because httprequest in
> Mongrel is different from webrick (has only 2 attributes body and
> params).
>
> thanks
>

By googling for "class Net::HTTP::Post multipart" you can find an
article on www.pivotalblabs.com that has a solution on how to enhance
net/https (by defining a 'text_to_multipart' method etc.). I used this
code snippet and it works really well.

BR Phil