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

半腔热情 提交于 2019-12-10 15:27:20

问题


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.


回答1:


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'




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/12149315/how-do-i-integrate-filepicker-io-with-google-app-engine-blobstore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!