How do I integrate filepicker.io with google app engine (blobstore)?
Asked Answered
B

3

4

So I'm trying to use filepicker.io with a GAE application. The filepicker widget returns the URL of the file that a user uploaded.

How do I then use this url to upload the file to GAE's blobstore?

The blobstore only supports "file uploading through a form", so the real question is how to fake a form POST containing the URL of a file.

Bor answered 27/8, 2012 at 20:41 Comment(0)
G
4

Faking a form post is possible, but it's much simpler to use the Files API

Edit:

To use the File API with urlfetch you would write something like this:

from __future__ import with_statement
from google.appengine.api import files
from google.appengine.api import urlfetch

url = "http://www.facebook.com/somephoto.png"
result = urlfetch.fetch(url)
if result.status_code not 200:
  return "some error"

# Create the file
file_name = files.blobstore.create(mime_type='application/octet-stream')

# Open the file and write to it
with files.open(file_name, 'a') as f:
  f.write(result.content)

# Finalize the file. Do this before attempting to read it.
files.finalize(file_name)

# Get the file's blob key
blob_key = files.blobstore.get_blob_key(file_name)

I haven't tested this - so if this doesn't work, let me know.

Also, I believe you can leave the mime_type as 'application/octet-stream' and App Engine will try and guess the proper type. If that doesn't work try changing it to 'image/png'. Or for a pdf the mime type would be 'application/pdf'

Goose answered 27/8, 2012 at 21:12 Comment(3)
Oh I was looking at that, but how would you write the contents of, let's say, a pdf to the blob object? It seems to only support text.Bor
Ah cool, it works! But the file is downloading (once you view it at the url) without an extension, so there's no way you'd know it's a .pdf. Is there a way of appending a file extension?Bor
@jellyksong Are you wanting to added the .pdf when serving from the Blobstore? Could you create a new question for this? Please include the code that you are using to server the file.Goose
P
1

The files API in the top voted answer is deprecated, so I wrote a gist to solve this problem on github. It takes the url of the image and posts it to your own servers using requests and poster.

https://gist.github.com/jmasonherr/6301914

Plunk answered 22/8, 2013 at 0:32 Comment(0)
I
0

We actually spent a good deal of time trying to find if we could hack around browsers to fake the exact sort of thing you describe, but to no avail. My recommendation would be write some quick back-end code like Kyle suggested to grab a file object from the url, but if you were really persistent you can actually fake your own multipart form request using ajax.

See XMLHttpRequest POST multipart/form-data and Is it possible to fake a multipart/form-data post with a jquery ajax call? for some ideas on how to do this

Immiscible answered 28/8, 2012 at 6:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.