0

这方面的新手,所以我可能会遗漏一些简单的东西。我正在尝试创建一个允许用户上传 csv 文档的应用程序,然后我对其进行解析,然后根据 csv 文件中的数据执行其他操作。

我用以下html 设置了一个非常基本的表单。

HTML

<html> 
    <head>
        <title>
            Paginator!
        </title>
    </head>

    <body>
        <form method="post">
            Enter name of page
            <br><input type="text" name="pagename">
            <br>
            Upload page info: <input type="file" name="csvfile" size="chars"><br>
            <input type="submit">
        </form>
    </body>
</html> 

Python代码:

import os
import csv
import webapp2

with open('index.html', 'rb') as f:
    HTML = f.read()
    print HTML

class MainPage(webapp2.RequestHandler):

    def get(self):
        self.response.headers['Content-Type'] = 'text/html'
        self.response.write(HTML)

    def post(self):
        self.response.write(self.request.get('pagename'))
        in_file = self.request.get('csvfile')

        if in_file:
            with open(in_file, 'rb') as f:
                csvreader = csv.reader(f, delimiter=',')
                data = [row for row in csvreader]

app = webapp2.WSGIApplication([('/', MainPage)],
                              debug=True)

所以我加载了带有表单信息的 HTML 文件,并在有 GET 请求时将其写入页面/

当我尝试处理 POST 请求时,问题就出现了。当我尝试通过“选择文件”按钮上传任何内容时,我收到来自 GoogleApp Engine 的无此类文件错误。

错误信息:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "C:\Users\Chris\Documents\Code\Python\iqm segmetizer\index.py", line 20, in post
    with open(in_file, 'rb') as f:
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py", line 610, in __init__
    super(FakeFile, self).__init__(filename, mode, bufsize, **kwargs)
IOError: [Errno 2] No such file or directory: u'test_page_info.csv'

我需要做什么才能允许用户(在这种情况下是我自己)能够通过表单上传 csv 文件?

4

1 回答 1

1

首先添加enctype="multipart/form-data"到您的表单:

<form method="post" enctype="multipart/form-data">
        Enter name of page
        <br><input type="text" name="pagename">
        <br>
        Upload page info: <input type="file" name="csvfile" size="chars"><br>
        <input type="submit">
</form>

这将确保您的浏览器正确编码要上传到服务器的文件。

然后修改您的 Python 代码以使用BlobstoreBlobReader

import csv
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
  def post(self):
    upload_files = self.get_uploads('csvfile')
    blob_info = upload_files[0]
    blob_reader = blobstore.BlobReader(blob_info.key())
    csv_data = blob_reader.read()
    csv_reader = csv.reader(csv_data)
    ...
于 2013-04-15T06:24:18.007 回答