我使用的谷歌应用程序引擎烧瓶和我拼命寻求帮助,以解决这个问题。 存储图像的数据存储中使用BlobProperty,它应该做的是这样的GAE文档会谈: -

class MyPics(db.Model):
      name=db.StringProperty()
      pic=db.BlobProperty()

现在的图像应该通过执行此存储在数据存储区: -

def storeimage():
    pics=MyPics()
    pics.name=request.form['name']
    uploadedpic=request.files['file']  #where file is the fieldname in the form of the                
                                        file uploaded
    pics.pic=db.Blob(uploadedpic)
    pics.put()
    redirect ... etc etc

但我无法做到这一点。我得到db.Blob接受一个字符串,但这个给予Filestorage对象...有人可以帮助我。此外,如果任何人都可以提示我如何上传流后的图像了。

有帮助吗?

解决方案

确定,所以这是怎么我终于解决了它: -

@userreg.route('/mypics',methods=['GET','POST'])
def mypics():    
   if request.method=='POST':
      mydata=MyPics()
      mydata.name=request.form['myname']
      file=request.files['file']
      filedata=file.read()
      if file:
         mydata.pic=db.Blob(filedata)
      mydata.put()
      return redirect(url_for('home'))
   return render_template('mypicform.html')

上面存储该文件作为数据存储中的团块,然后其可通过以下FUNC检索: -

@userreg.route('/pic/<name>')
def getpic(name):
     qu=db.Query(MyPics).filter('name =',name).get()
     if qu.pic is None:
         return "hello"
     else:
         mimetype = 'image/png'
         return current_app.response_class(qu.pic,mimetype=mimetype,direct_passthrough=False)

其他提示

您应该考虑使用BlobStore存储数据。取而代之的是db.Blob你会使用blobstore.BlobReferenceProperty的http://代码.google.com /应用服务引擎/文档/蟒/数据存储/ typesandpropertyclasses.html#BlobReferenceProperty

上传和下载是很容易的,如下所示: HTTP: //code.google.com/appengine/docs/python/blobstore/overview.html#Complete_Sample_App

我具有以下模型

class Picture(db.Model):    
    data = db.BlobProperty()
    ext = db.StringProperty()
    content_type = db.StringProperty()

和存储它使用下一个代码:

def create_picture():
    if request.files['file']:                       
        picture.data = request.files['file'].read()
        picture.ext = request.files['file'].filename.rsplit('.', 1)[1]
        picture.content_type = request.files['file'].content_type
        picture.put()
        flash(u'File uploaded', 'correct')
        return redirect(url_for("edit_picture", id=picture.key()))
    else:
        return render_template('admin/pictures/new.html', title=u'Upload a picture', message=u'No picture selected')

要呈现的缩略图可以使用下一个代码:

@frontend.route('/thumbnail/<id>/<width>x<height>.<ext>')
def thumbnail(id, width, height, ext):
    key = db.Key(id)
    picture = Picture.get(key)
    img = images.Image(picture.data)

    if width != '0':
        w = int(width)
    else:
        w = img.width

    if height != '0':
        h = int(height)
    else:
        h = img.height

    if img.height > h and h != 0:
        w = (int(width) * img.width) / img.height;

    if img.width > w:
        h = (int(height)  * img.height) / img.width;

    thumb = images.resize(picture.data, width=w, height=h)    
    response = make_response(thumb)
    response.headers['Content-Type'] = picture.content_type
    return response
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top