views:

66

answers:

1

I don't know why I cant upload my file? When I hit submit instead of redirecting to the default page(which is http://localhost:8082) , it redirects to http://localhost:8082/sign. I didn't build no such path, so it return link broken. Here's my html:

<form action="/sign" enctype="multipart/form-data" method="post">
                <div><label>Excel file submit:</label></div>
                <div><input type="file" name="excel"/></div>
                <div><input type="submit" value="Upload file"></div>
            </form>

my app.yaml:

application: engineapp01
version: 1
runtime: python
api_version: 1

handlers:
- url: /js
  static_dir: js
- url: /css
  static_dir: css
- url: .*
  script: main.py

main.py:

import os;
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from mmap import mmap,ACCESS_READ
from xlrd import open_workbook

class Account(db.Model):
    name = db.StringProperty()
    type = db.StringProperty()
    no = db.IntegerProperty()
    co = db.IntegerProperty()

class MyFile(db.Model): 
    filedata = db.BlobProperty()

class MainHandler(webapp.RequestHandler):
    def get(self):
        #delete all old temporary entries
        query = Account.all()
        results = query.fetch(limit=40)
        db.delete(results)

        #temporary entry
        acc = Account(name='temporaryAccountName',
                    type='temporaryType',
                    no=0,
                    co=500)
        acc.put()
        temp = os.path.join(os.path.dirname(__file__),'templates/index.htm')

        tableData = ''
        query = Account.all()
        query.order('name')
        results = query.fetch(limit=20)
        for account in results:
            tempStr= """
                    <tr>
                        <td align='left' valign='top'>%s</td>
                        <td align='left' valign='top'>%s</td>
                        <td align='left' valign='top'>%d</td>
                        <td align='left' valign='top'>%d</td>
                    </tr>""" % (account.name,account.type,account.no,account.co)
            tableData = tableData + tempStr

        outstr = template.render(
                temp,
                {'tabledata':tableData})
        self.response.out.write(outstr)

    def post(self):
        myFile = MyFile()
        excel = self.request.get("excel")
        myFile.fileData = db.Blob(excel)
        myFile.put()
        self.redirect('/')

def main():
    application = webapp.WSGIApplication([('/', MainHandler)],
                                            debug=True)
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()
+1  A: 

Why are you sending the form data to a page that doesn't exist?

Try changing the HTML to:

<form action="/" enctype="multipart/form-data" method="post">
...

...since "/" is where your form-handling code resides.

Adam Bernier
woot! that solves my problem. Still have to check the upload part though. Is there any fast way to do this?
Khoi
Check if the upload completed successfully in the admin console.
Adam Bernier
that was damn quick! Thanks a lot.
Khoi
Cheers, mate. Happy GAE developing. What a great platform, eh?
Adam Bernier