I'm completely new to GAE and trying to figure some things out. I'm trying
to write a small app which will allow a user to upload files, and display a
list of those files already uploaded. I am having trouble getting my app to
list the files already uploaded.
The code works in so far as it allows me to upload a file, but it does not
print the list of files already uploaded. Any ideas where I'm going wrong?
My Code:
from google.appengine.ext import blobstore
from google.appengine.ext import ndb
from google.appengine.ext.webapp import blobstore_handlers
import webapp2
class DatastoreFile(ndb.Model):
data = ndb.BlobProperty(required=True)
mimetype = ndb.StringProperty(required=True)
filename = ndb.StringProperty(required=True)
timestamp = ndb.DateTimeProperty(required=True,auto_now_add=True)
class MainHandler(webapp2.RequestHandler):
def get(self):
upload_url = blobstore.create_upload_url('/upload_file')
files = DatastoreFile.query().order(-DatastoreFile.timestamp)
for file in files:
self.response.write('<li> %s' % file.filename)
self.response.write("""
<html><body>
<form action="{0}" method="POST" enctype="multipart/form-data">
Upload File: <input type="file" name="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body></html>""".format(upload_url))
class UploadFile(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
file_data = self.request.POST.get('file_data', None)
if file_data is not None:
entity = DatastoreFile(
data=file_data.value,
filename=file_data.filename,
mimetype=file_data.type
)
entity.put()
response = {
'filename': entity.filename,
'timestamp': str(entity.timestamp),
'key': entity.key.urlsafe()
}
else: # no files were received
response = {'error': 'file not received'}
self.redirect('/')
app = webapp2.WSGIApplication([
('/', MainHandler),
('/upload_file', UploadFile),
], debug=True)
--
You received this message because you are subscribed to the Google Groups
"Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/google-appengine.
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-appengine/0e54b8d9-9595-42b9-8866-26292701e208%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.