Only just got a chance to test this out now, but that does indeed fix the problem!
Thank you so much for your help. I really appreciate it! On Monday, May 16, 2016 at 11:06:18 PM UTC+1, Jason Collins wrote: > > [... and now for the exciting conclusion to that last post ....] > > > class UploadFile(blobstore_handlers.BlobstoreUploadHandler): > def post(self): > if self.get_uploads(): > upload = self.get_uploads()[0] # if you only have one file > input on your form > entity = DatastoreFile( > data=upload.key(), # this is the BlobKey > filename=upload.filename, > mimetype=upload.content_type > ) > entity.put() > ... > > NOTE that you do not get the file bytes themselves. They are "peeled off" > the POST request and stored into Blobstore - the key() is the reference to > the bytes. > > If you want to store bytes directly into Datastore yourself, then you > don't want the Blobstore API at all and you need to perform the POST arg > processing yourself. > > > On Monday, 16 May 2016 14:57:55 UTC-7, Jason Collins wrote: >> >> The __BlobInfo__ will be written regardless. >> >> So, given that your DatastoreFile entities are not being created, look to >> the post() method of UploadFile. >> >> The parent class BlobstoreUploadHandler gives you a method called >> get_uploads() - use this instead of looking at the POST args. >> >> So you have something like: >> >> class UploadFile(blobstore_handlers.BlobstoreUploadHandler): >> def post(self): >> >> >> >> >> On Monday, 16 May 2016 14:51:15 UTC-7, Peter S wrote: >>> >>> Thanks for that! Seems that it's still not displaying my list though. >>> After some investigation I see that my files are being uploaded to the >>> datastore as __BlobInfo__ objects, and not as my 'DatastoreFile' objects >>> that I had expected. Any ideas why that is happening? Is there something I >>> can do to ensure that they're uploaded a 'DatastoreFile' objects, or >>> failing that, how would I go about listing the __BlobInfo objects which >>> have been uploaded? >>> >>> >>> <https://lh3.googleusercontent.com/-c7w5tyjV1rw/VzpAmWCLdPI/AAAAAAAAAAg/nc76zRP7wnoSsCuE64E_OHAQENMxdarVACLcB/s1600/Capture.JPG> >>> >>> >>> On Sunday, May 15, 2016 at 4:51:42 PM UTC+1, Jason Collins wrote: >>>> >>>> I think your <html> and <body> tags are out of order (try doing a View >>>> Source on your page to see if the <li> filenames are there). And you >>>> should >>>> include a <ul> >>>> >>>> Just reorder things a bit, something like: >>>> >>>> class MainHandler(webapp2.RequestHandler): >>>> def get(self): >>>> upload_url = blobstore.create_upload_url('/upload_file') >>>> >>>> * self.response.write("""* >>>> *<html><body>* >>>> *<ul>* >>>> *""")* >>>> >>>> files = DatastoreFile.query().order(-DatastoreFile.timestamp) >>>> for file in files: >>>> self.response.write('<li> %s' % file.filename) >>>> >>>> self.response.write(""" >>>> *</ul>* >>>> <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)) >>>> >>>> >>>> Also, look into using templates like Jinja as it will make it much >>>> easier to write and debug stuff like this. >>>> >>>> >>>> >>>> On Friday, 13 May 2016 18:43:30 UTC-7, Peter S wrote: >>>>> >>>>> >>>>> 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/706f1303-1cee-4259-bc9a-009d2d217d8f%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
