Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. # Render the template for the upload form:
  2.  
  3. @app.route("/upload")
  4. def upload():
  5. uploadUri = blobstore.create_upload_url('/submit', gs_bucket_name=BUCKET_NAME)
  6. return render_template('upload.html', uploadUri=uploadUri)
  7.  
  8. # Place your uploadUri in the form path (html):
  9.  
  10. '''<form action="{{ uploadUri }}" method="POST" enctype="multipart/form-data">'''
  11.  
  12.  
  13. # Here is the function to handle the upload of the image (I return the blob_key for practical reasons, replace it with your template):
  14.  
  15. @app.route("/submit", methods=['POST'])
  16. def submit():
  17. if request.method == 'POST':
  18. f = request.files['file']
  19. header = f.headers['Content-Type']
  20. parsed_header = parse_options_header(header)
  21. blob_key = parsed_header[1]['blob-key']
  22. return blob_key
  23.  
  24. # Now say you serve your images with a path like this:
  25. # /img/imagefilename
  26. # Then your image serving function is :
  27.  
  28. @app.route("/img/<bkey>")
  29. def img(bkey):
  30. blob_info = blobstore.get(bkey)
  31. response = make_response(blob_info.open().read())
  32. response.headers['Content-Type'] = blob_info.content_type
  33. return response
  34.  
  35. # Finally, anywhere you need to display an image in a template, you simply put the code:
  36. '''<img src="/img/{{ bkey }} />'''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement