为什么我的文件信息会重复上传文件

时间:2016-05-20 16:10:57

标签: python python-2.7 google-app-engine google-cloud-datastore app-engine-ndb

我已经在表格中列出了文件列表,但是当我上传文件时,有关该文件的数据会重复到其他文件,导致重复日期,但是在blobstore查看器中我可以看到正在上传的文件。 如有必要,请链接到下载文件:https://drive.google.com/file/d/0B-FjILy0V_UUVERVVDhTM3ZlU0k/view?usp=sharing

Main.py

Dim rng As Range

Set rng = Range(Cells(6,1), Cells(xlrow, xlcol))

rng.font.size = 9
rng.Borders.Color = RGB(0,0,0)
rng.Borders(xlEdgeTop).Weight = xlThick
etc...

HTML

import os
import jinja2
import webapp2
from google.appengine.ext import blobstore
from google.appengine.ext import ndb
from google.appengine.ext.webapp import blobstore_handlers
import urllib
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

jinja_env = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
autoescape=True)

# Key to serve as a parent
PARENT_KEY = ndb.Key("Entity", "files_root")


# This datastore model keeps track of which users uploaded which photos.
class UserFiles(ndb.Model):
file1 = ndb.StringProperty()
key1 = ndb.StringProperty()
create1 = ndb.StringProperty()
type1 = ndb.StringProperty()

class PhotoUploadFormHandler(webapp2.RequestHandler):
def get(self):
    file_query = UserFiles.query(ancestor=PARENT_KEY)
    upload_url = blobstore.create_upload_url('/upload')
    templateHome = jinja_env.get_template("templates/page.html")
    self.response.out.write(templateHome.render({"file_query": file_query}).format(upload_url))
    for b in blobstore.BlobInfo.all():
        self.response.out.write('<li><a href="/serve/%s' % str(b.key()) + '">' + str(b.filename) + '</a>')


# [START upload_handler]
class PhotoUploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
    try:

        for b in blobstore.BlobInfo.all():
            key = str(b.key)
            fileName = str(b.filename)
            dateCreated = str(b.creation)
            typeOfFile = str(b.content_type)

        files = UserFiles(
            parent=PARENT_KEY,
            file1=fileName,
            create1=dateCreated,
            type1=typeOfFile)

        files.put()

        self.redirect(self.request.referer)

    except:
        self.error(500)
# [END upload_handler]

app = webapp2.WSGIApplication([
('/', PhotoUploadFormHandler),
('/upload', PhotoUploadHandler)
], debug=True)

1 个答案:

答案 0 :(得分:1)

为什么要为每次上传迭代所有现有的blobstore条目?

for b in blobstore.BlobInfo.all(): # I don't think you need to do this

您可以通过以下方式获取上传的blob:

upload = self.get_uploads()[0]

然后使用上面的UserFiles对象创建upload条目。