Html 如何将图像从客户端表单上传到Google云存储桶?
有一个表单可以上载图像。现在我想把它存储在谷歌云存储中,然后在页面上打印出来。表格为:-Html 如何将图像从客户端表单上传到Google云存储桶?,html,google-app-engine,python-2.7,google-cloud-storage,webapp2,Html,Google App Engine,Python 2.7,Google Cloud Storage,Webapp2,有一个表单可以上载图像。现在我想把它存储在谷歌云存储中,然后在页面上打印出来。表格为:- <form action="http://master-engine-799.appspot.com/uploadimage" method="POST" enctype="multipart/form-data"> Upload File: <input type="file" name="file"><br> <input type="submit" name=
<form action="http://master-engine-799.appspot.com/uploadimage" method="POST" enctype="multipart/form-data">
Upload File: <input type="file" name="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
create_upload_url提供的upload_url是希望用作表单本身上的“操作”的url,而不是/uploadimage。因此,您的HTML表单应该更像:
<form action="CONTENTS_OF_UPLOAD_URL_HERE" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
形式是这样的
<html>
<body>
<form action="http://your-app-id.appspot.com/myimagehandler" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br>
File Name : <input type="text" name="filename1"> <input type="submit" name="submit" value="Submit"> </form>
</body>
</html>
文件名:
实际上表单在客户端。从表单中,它将调用服务。那么我将如何提供上传url,因为它将被动态创建。。。
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file') # 'file' is file upload field in the form
blob_info = upload_files[0]
self.redirect('/serve/%s' % blob_info.key())
from __future__ import with_statement
import cloudstorage as gcs
import webapp2
import logging
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
def CreateFile(filename,imageFile):
with gcs.open(filename, 'w', content_type = 'image/jpeg') as f:
f.write(imageFile)
f.close()
blobstore_filename = '/gs' + filename
return blobstore.create_gs_key(blobstore_filename)
class MyImageHandler(webapp2.RequestHandler):
def post(self):
bucket='yourbucketname'
imageFile = self.request.get('file')
naemofFile=self.request.get('filename1')
fileName='/yourbucketname'+'/'+naemofFile
blob_key = CreateFile(fileName,imageFile)
logging.info("Blob-Key "+blob_key)
imageUrl = 'https://%(bucket)s.storage.googleapis.com/%(file)s' % {'bucket':bucket, 'file':naemofFile}
app = webapp2.WSGIApplication([('/myimagehandler', MyImageHandler)],
debug=True)
<html>
<body>
<form action="http://your-app-id.appspot.com/myimagehandler" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br>
File Name : <input type="text" name="filename1"> <input type="submit" name="submit" value="Submit"> </form>
</body>
</html>