Please note that I neglected to encode data below:
data = { "file" : open(full_path, "rb") }
data = urllib.urlencode(data)

Just in case,
Miguel

On Wed, Dec 9, 2009 at 12:13 PM, Miguel Lopes <[email protected]> wrote:
> On Wed, Dec 9, 2009 at 10:26 AM, Miguel Lopes <[email protected]> wrote:
>> Hi,
>>
>> I've been trying to upload a file via a webservice, without any success.
>> I'm exposing a serv_upload() action via @service.run. How can I get a
>> handle of the file in the serv_upload() action?
>>
>> I'm using (trying) to use urllib2 to get the file from the server.
>
> Solved it. My fault for having an outdated version of web2py with a
> bad upload bug.
> For those eventually interested and for any improvement suggestions, here's 
> how:
>
> 1. I used the MultipartPostHandler module by Will Holcomb modified by
> Brian Schneider. You can find it here:
> http://peerit.blogspot.com/2007/07/multipartposthandler-doesnt-work-for.html
>
> 2. For the client:
> def upload_file(self, full_path):
>    import MultipartPostHandler, urllib2, cookielib
>    cookies = cookielib.CookieJar()
>    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies),
>
> MultipartPostHandler.MultipartPostHandler)
>    data = { "file" : open(full_path, "rb") }
>    try:
>        response =
> opener.open("http://127.0.0.1:8000/myservice/default/call/run/serv_upload/";,
> data)
>    except HTTPError:
>        log.write(response.read())
>
> 3. The easy part :-) the action in web2py:
> @service.run
> def upload_service():
>    import os
>    infile = request.vars.file
>    filename = infile.filename
>    path = os.path.join(request.folder,'private', filename)
>    f = open(path, 'wb')
>    f.write(infile.file.read())
>    f.close()
>
> HTH & Txs for any suggestions,
> Miguel
>

--

You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en.


Reply via email to