Re: [google-appengine] Uploading Files

4,167 views
Skip to first unread message
Message has been deleted

Venkatesh Rangarajan

unread,
Apr 11, 2008, 2:11:55 AM4/11/08
to Google App Engine
just to clarify. i do commit it

class MainPage(webapp.RequestHandler):
  def post(self):
   document=Document()
   document.srcfile=db.Blob(self.request.get('upfile'))
   document.put()

And here is the traceback

Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\__init__.py", line 486, in __call__
handler.post(*groups)
File "C:\Program Files\Google\google_appengine\demos\rapid\upload.py", line 18, in post
document=Document()
File "C:\Program Files\Google\google_appengine\google\appengine\ext\db\__init__.py", line 553, in __init__
prop.__set__(self, value)
File "C:\Program Files\Google\google_appengine\google\appengine\ext\db\__init__.py", line 370, in __set__
value = self.validate(value)
File "C:\Program Files\Google\google_appengine\google\appengine\ext\db\__init__.py", line 1702, in validate
value = super(BlobProperty, self).validate(value)
File "C:\Program Files\Google\google_appengine\google\appengine\ext\db\__init__.py", line 397, in validate
raise BadValueError('Property %s is required' % self.name)
BadValueError: Property srcfile is required



Patrick Crosby

unread,
Apr 11, 2008, 10:13:59 AM4/11/08
to Google App Engine
remove the db.Blob when you assign it:

document.srcfile = self.request.get('upfile')

should work...

On Apr 11, 2:11 am, "Venkatesh Rangarajan"

Venkatesh Rangarajan

unread,
Apr 11, 2008, 10:22:29 AM4/11/08
to google-a...@googlegroups.com
I have tried that before. Same error.

:(

Fabricio Zuardi

unread,
Apr 11, 2008, 12:41:26 PM4/11/08
to google-a...@googlegroups.com
Is file uploaded (from a html form to the datastore as a blob)
supported at all? I know there is an open issue for that at
http://code.google.com/p/googleappengine/issues/detail?id=78

--
Fabricio C Zuardi
http://cchits.org

Venkatesh Rangarajan

unread,
Apr 11, 2008, 1:16:37 PM4/11/08
to google-a...@googlegroups.com
Can somebody please help me with this ?

If file uploading to Datastore as a blob supported ? I have tried various different things and can't get it to work.

Andrew Burton

unread,
Apr 11, 2008, 1:21:25 PM4/11/08
to Google App Engine
On Apr 11, 12:16 pm, "Venkatesh Rangarajan"
<venkatesh.rangara...@gmail.com> wrote:
> Can somebody please help me with this ?

Your code looks good, but I think your HTML may be the problem. Can
you post the template?

Venkatesh Rangarajan

unread,
Apr 11, 2008, 1:24:56 PM4/11/08
to google-a...@googlegroups.com
Here is the upload code.


self.response.out.write("""
          <form method='POST' enctype='multipart/form-data' action='process'>
      File to upload: <input type=file name=upfile><br>
      Notes about the file: <input type=text name=note><br><br>
      <input type=submit value=Press> to upload the file!</form>""")

Source from the browser


<form method='POST' enctype='multipart/form-data' action='process'>
File to upload: <input type=file name=upfile><br>
Notes about the file: <input type=text name=note><br><br>
<input type=submit value=Press> to upload the file!</form>

Andrew Burton

unread,
Apr 11, 2008, 2:22:41 PM4/11/08
to Google App Engine
Okay, I think it's that you made srcfile "required=True". Take that
out and see if it works.

Venkatesh Rangarajan

unread,
Apr 11, 2008, 2:24:39 PM4/11/08
to google-a...@googlegroups.com
Tried that. Doesn't work.
Message has been deleted

PatrickNegri

unread,
Apr 11, 2008, 2:42:49 PM4/11/08
to Google App Engine
Excelent work Andrew.

Take a look at my tutorial here in forum about XML too, i am
explaining how to add code library to the AppEngine filesystem.

On Apr 11, 2:30 pm, Andrew Burton <jarodgo...@gmail.com> wrote:
> Let me post my code that works, and we can compare for differences.
> Ignore SplitFile, it's just a JavaScript function gets the file name
> and extension from the upload field. Also, except for a couple of
> spacing changes in the templates, this is the code I have running athttp://jarod-guestbook.appspot.com.
>
> class File(db.Model):
> filename = db.StringProperty(multiline=False)
> image = db.BlobProperty()
> ext = db.StringProperty(multiline=False)
> date = db.DateTimeProperty(auto_now_add=True)
>
> class UploadFile(webapp.RequestHandler):
> def post(self):
> newfile = File()
>
> newfile.filename = self.request.get('filename')
> newfile.ext = self.request.get('ext')
> newfile.image = db.Blob(self.request.get('upload'))
> newfile.put()
>
> self.redirect("/files")
>
> <form action="upload" method="post" enctype="multipart/form-data">
> <div><input type="file" id="fileUpload" name="upload"
> onchange="SplitFile();" /></div>
> <div><input type="submit" value="Upload" /></div>
> <input type="hidden" id="fileName" name="filename" value="" />
> <input type="hidden" id="fileExt" name="ext" value="" />
> </form>

Venkatesh Rangarajan

unread,
Apr 11, 2008, 7:34:07 PM4/11/08
to google-a...@googlegroups.com
Thanks Andrew.

I no longer get the error. But I am not sure if is uploading the document to the data store.

I am trying to publish the documents and it always returns NULL values.

Model

class Document(db.Model):
  author = db.UserProperty()
  note = db.StringProperty()
  srcfile =db.BlobProperty(default=None)
  cfilename=db.StringProperty()
  xName=db.StringProperty()

Retrieve Document (key is xName which is unique)

class List(webapp.RequestHandler):
  def get(self):
    self.response.headers['Content-Type'] = "application/octet-stream"
    query = db.Query(Document)
    query= Document.all()
    query.filter('xName=', "'"+self.request.get('f')+"'")
    document=query.fetch(limit=1) 
    for documents in document:
      self.response.out.write(documents.srcfile)

It always returns a zero byte document. So in effect, either the document is not getting uploaded or my display code is busted.



Mathias Dahl

unread,
Apr 12, 2008, 5:44:04 AM4/12/08
to Google App Engine
> class File(db.Model):
> filename = db.StringProperty(multiline=False)
> image = db.BlobProperty()
> ext = db.StringProperty(multiline=False)
> date = db.DateTimeProperty(auto_now_add=True)
>
> class UploadFile(webapp.RequestHandler):
> def post(self):
> newfile = File()
>
> newfile.filename = self.request.get('filename')
> newfile.ext = self.request.get('ext')
> newfile.image = db.Blob(self.request.get('upload'))
> newfile.put()
>
> self.redirect("/files")

I also have a problem with this. My upload code looks like yours so it
should work:

class MyFile(db.Model):
filedata = db.BlobProperty
filename = db.StringProperty(multiline=False)
date = db.DateTimeProperty(auto_now_add=True)

class UploadFile(webapp.RequestHandler):
def post(self):
myfile = MyFile()
myfile.filedata = db.Blob(self.request.get('file'))
myfile.filename = self.request.get('filename')
myfile.put()
self.redirect('/')

HTML form:

self.response.out.write("""
<form action="/upload" method="post" enctype="multipart/form-
data">
<div>Myfile:<br><input type="file" name="file" size="50"/
></div>
<div>Description (optional): <input type="text"
name="filename" value=""></div>
<div><input type="submit" value="Upload file"></div>
</form>""")

I suspect the problem is when I try to download it:

class Download(webapp.RequestHandler):
def get(self):
id = self.request.get('id')
myfile = MyFile.get_by_id(int(id))
if myfile:
self.response.headers['Content-Type'] = 'image/jpg'
self.response.out.write(myfile.filedata)
else:
self.response.out.write('Could not find file %s.' % id)

The result in the browser is a string, actually the URL to my download
page!

Could you post your download code (your /image handler) as well?

Mathias Dahl

unread,
Apr 12, 2008, 1:00:05 PM4/12/08
to Google App Engine
> class MyFile(db.Model):
> filedata = db.BlobProperty
> filename = db.StringProperty(multiline=False)
> date = db.DateTimeProperty(auto_now_add=True)

I found the problem. My bad, of course. It was hard to spot. Here is
the correct code:

filedata = db.BlobProperty()

See the missing parentheses? Yup. Quite a difference :)
Reply all
Reply to author
Forward
0 new messages