我设法让上传工作,但现在我必须让文件显示在列表中(文件名,上传/创建日期,文件类型和下载选项)我无法找到如何使用它
我知道我正在寻找:文件名(可以链接查看图片和左侧的复选框,您可以在下面的html中看到),内容类型和创建日期。
HTML:
<form action="/download" method="post">
<table id="downloadFile" width="100%">
<tr>
<td>
<table id="downloadFileDetails">
<tr id="dld">
<td id="dld" width="2%"></td>
<td id="dld" width="80%">File Name</td>
<td id="dld" width="10%">Upload Date</td>
<td id="dld" width="8%">Format</td>
</tr>
<tr>
<td id="dld" style="vertical-align: middle; text-align: center "><input type="checkbox" name="checkFile" > </td>
<td id="dld">test</td>
<td id="dld">test</td>
<td id="dld">test</td>
</tr>
{% for file in file_query %}
<tr>
<td id="dld" style="vertical-align: middle; text-align: center "><input type="checkbox" name="checkFile" > </td>
<td id="dld">{{file.user}}</td>
<td id="dld">{{file.blob_key}}</td>
<td id="dld">{{file.last_touch}}</td>
</tr>
{% endfor %}
</table>
</td>
</tr>
<tr>
<td><input type="submit" value="Download File" name="submit"></td>
</tr>
</table>
</form>
Python代码:
import os
from google.appengine.api import users
from google.appengine.ext import blobstore
from google.appengine.ext import ndb
from google.appengine.ext.webapp import blobstore_handlers
import webapp2
import jinja2
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 UserPhoto(ndb.Model):
user = ndb.StringProperty()
blob_key = ndb.BlobKeyProperty()
filename = ndb.StringProperty()
#last_touch = ndb.DateTimeProperty(auto_now_add = True)
class PhotoUploadFormHandler(webapp2.RequestHandler):
def get(self):
file_query = UserPhoto.query(ancestor = PARENT_KEY)#.order(-UserPhoto.last_touch)
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))
# [START upload_handler]
class PhotoUploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
try:
upload = self.get_uploads()[0]
user_photo = UserPhoto(parent = PARENT_KEY,
user=users.get_current_user().user_id(),
blob_key=upload.key(),
filename = upload.filename())
user_photo.put()
self.redirect(self.request.referer)
except:
self.error(500)
# [END upload_handler]
app = webapp2.WSGIApplication([
('/', PhotoUploadFormHandler),
('/upload', PhotoUploadHandler)
#('/view_photo/([^/]+)?', ViewPhotoHandler),
], debug=True)
我收到日志中的消息:
INFO 2016-05-14 20:54:05,506 module.py:787] default: "GET / HTTP/1.1" 200 1997
INFO 2016-05-14 20:54:05,525 module.py:787] default: "GET /static/styles.css HTTP/1.1" 304 -
INFO 2016-05-14 20:54:05,546 module.py:787] default: "GET /favicon.ico HTTP/1.1" 200 8348
INFO 2016-05-14 20:54:11,806 module.py:787] default: "POST /upload HTTP/1.1" 500 -
答案 0 :(得分:0)
您似乎正在从the docs重新创建示例应用程序。这个例子掩盖了一条重要的信息,可以找到here:
get_uploads()方法返回一个BlobInfo对象列表,一个用于请求中的每个上传文件。
因此,如果您想捕获文件名,例如,您需要在上传处理程序中执行以下操作(并相应地调整UserPhoto
模型):
upload = self.get_uploads()[0]
user_photo = UserPhoto(
user=users.get_current_user().user_id(),
blob_key=upload.key(),
filename=upload.filename
)
user_photo.put()
get_uploads
方法返回BlobInfo
个对象的列表。这些对象的文档可以在这里找到:
注意:我没有任何关于AppEngine上传的经验,并且此代码尚未经过测试,因此可能会有一些细节。我只是去找文档。但我怀疑它遇到了根本问题。如果需要更改任何细节,请告诉我。