我正在尝试在 GAE python 中读取简单的文件。我想允许用户直接从他们的磁盘上传一个 csv 文件并让它显示在一个文本字段中。这样做我遇到了极大的麻烦。任何帮助,将不胜感激。如果可能,我宁愿避免使用 blobstore。我不想永远保存数据,我只想用它来让人们轻松地填充文本区域。
这是我的表格的样子
<div class = "section hidden" id ="file_choice">
<form action="post"enctype="multipart/form-data">
Choose a '.CSV' file you want to upload or convert.
<br>
<input type = "file" id = "filein" name = "filecsv"/>
<br>
<i>if you are unsure how to convert your file to csv click <a hred = "/instructions">here</a> </i>
<br>
<input type="Submit" name="submit_final"/>
<br>
Alternatively you can use this text box to copy paste from any editor.
<textarea name="txtcsv" cols="150" rows="30">{{myFile}}</textarea>
</form>
</div>
这是我的代码的样子,
class Handler(webapp2.RequestHandler):
#wraps response.out.write for ease of typing
def write(self, *a, **kw):
self.response.out.write(*a,**kw)
#renders a template as a string
def render_str(self,template,**params):
t = jinja_env.get_template(template)
return t.render(params)
#renders the template to screen using write
def render(self,template,**kw):
self.write(self.render_str(template,**kw))
class MainHandler(Handler):
def get(self):
self.render('parseForm.html')
def post(self):
test =self.request.POST['filecsv'].getvalue()
self.render('parseForm.html',myFile=test,error="ERRORRRR")
现在 textarea 以文件名的形式输出。