无法在Google App Engine中上传文件

时间:2010-10-02 05:21:45

标签: python google-app-engine

我不知道为什么我无法上传我的文件?当我点击提交而不是重定向到默认页面(http://localhost:8082)时,它会重定向到http://localhost:8082/sign。我没有建立这样的路径,所以它返回链接断开。这是我的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>

我的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 个答案:

答案 0 :(得分:1)

为什么要将表单数据发送到不存在的页面?

尝试将HTML更改为:

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

...因为“/”是您的表单处理代码所在的位置。