Handling File Uploads

61 views
Skip to first unread message

crazywizard

unread,
Jun 13, 2011, 2:36:43 AM6/13/11
to Google App Engine
I need to handle uploaded files on my server. Problem is, the uploads
come from other user applications that I have no control over. My
problem centers around how to get the uploaded file without
referencing or knowing the index/key name of the file.
My app engine python code:
file=self.request.POST['file']

As you can see, I have to explicitly define the key name as 'file'.
However, the uploading application doesn't have this defined.
Pys60 code:
conn=httplib.HTTPConnection("www.myserver.com")
conn.request("POST", "/upload", 'file")

This only defines the method, destination, and file object. How can I
still get the POSTED file?
Please do not ask me to change or manipulate the client upload
procedures/functions because I don't have access to them.!

Nick Johnson (Google)

unread,
Jun 14, 2011, 12:41:59 AM6/14/11
to google-appengine
Hi,

In the example below, the file isn't being uploaded as part of a form - it's simply the contents of the request body. You can access it with self.request.body.

-Nick Johnson


--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To post to this group, send email to google-a...@googlegroups.com.
To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.




--
Nick Johnson, Developer Programs Engineer, App Engine


Tim

unread,
Jun 14, 2011, 7:33:41 AM6/14/11
to google-a...@googlegroups.com

On the topic of file uploads (if you'll pardon slight hijacking of the thread), when using the provided webapp framework, I couldn't find a way to get hold of the type and filename parameters from a file submitted as part of a multipart form request, so had to resort to a "plain CGI" handler

def main():
    form = cgi.FieldStorage()
    if "uploadedfile" in form:
        i = form["imagefile"]
        filename = i.filename
        filetype     = i.type
        filecontents  = i.value

No great hardship (and I expect these extra values can't always be trusted - I only use them as part of reporting back to the user what's been done), but does anyone know how to get these values using webapp, or do I have to leave this page as it stands?

--
T

Nick Johnson (Google)

unread,
Jun 15, 2011, 2:01:37 AM6/15/11
to google-appengine
http://blog.notdot.net/2009/9/Handling-file-uploads-in-App-Engine

-Nick

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-appengine/-/o9xygy8wvA8J.

To post to this group, send email to google-a...@googlegroups.com.
To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.

Tim

unread,
Jun 15, 2011, 6:21:09 AM6/15/11
to google-a...@googlegroups.com

Thanks for that Nick.

Seems I must have been sleeping when looking at that part of the webapp docs - getting the appropriate field from the request.POST

  file = self.request.POST["filefield"]
  
gets me an object with properties "filename" and "type" and "value" (for the actual decoded data) - Nick's blog post then shows how to store this in the datastore.

Apologies for being so thick

--
Tim

David Ngugi

unread,
Jun 16, 2011, 2:12:20 PM6/16/11
to google-a...@googlegroups.com
Hi NIck, if I do self.request.body, I also get a lot of meta-data
attached to the file's contents which I don't want. Is it possible to
just get the file part only?

> --
> You received this message because you are subscribed to the Google Groups
> "Google App Engine" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/google-appengine/-/daSNSHmoQEEJ.


> To post to this group, send email to google-a...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-appengi...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/google-appengine?hl=en.
>
>


--
Check out my blog:
http://www.crazyblackkenyanwizard.blogspot.com

Nick Johnson (Google)

unread,
Jun 17, 2011, 2:42:37 AM6/17/11
to google-appengine
Hi David,

What does this metadata look like? If it's HTTP-style headers, then it probably _is_ a multipart form, and you should be handling it in the standard way, using self.request.POST or self.request.get().

-Nick
Reply all
Reply to author
Forward
0 new messages