问题描述
我试图在GAE Python中读取简单的文件。我想让用户直接从他们的磁盘上传一个csv文件,并将其显示在文本字段中。这样做我有一个非常大的麻烦。任何帮助,将不胜感激。如果可能,我宁愿避免使用blobstore。我不想永久保存数据,我只是想用它来让人们很容易地填写文本。这是我的表单看起来像
< div class =section hiddenid =file_choice>
< form action =postenctype =multipart / form-data>
选择您要上传或转换的'.CSV'文件。
< br>
< input type =fileid =fileinname =filecsv/>
< br>
< i>如果您不确定如何将您的文件转换为csv,请点击此处< a hred =/ instructions>< / a> < I> /;
< br>
< input type =Submitname =submit_final/>
< br>
或者,您可以使用此文本框从任何编辑器复制粘贴。
< / form>
< / div>
继承我的代码的样子,
<$
$ b $ w
#wraps response.out.write,以方便输入
def write(self,* a,* * kw):
self.response.out.write(* a,** kw)
#以字符串形式显示模板
def render_str(self,template,* * params):
t = jinja_env.get_template(template)
return t.render(params)
使用write $ b $ ,模板,** 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获取输出作为文件名。
lockquote
上传的文件可以直接使用cgi.FieldStorage(请参阅cgi模块)
实例在request.POST。
我要跟随这个帖子与我自己的问题,但如果你使用:
test = self.request.POST ['filecsv']。
pre>
而不是
getvalue()
(它会为我引发一个错误),那么您将看到呈现的输出的csv文件。I'm trying to get simple file reading in GAE python. I want to allow users to upload a csv file directly from their disk and have it display in a text field. I am having an outrageous amount of trouble doing so. Any help would be appreciated. I would prefer to avoid using the blobstore if possible. I don't want to save the data forever, I just want to use it to allow people to fill the textarea easily.
Here's what my form looks like
<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>
Heres what my code looks like,
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")
Right now the textarea gets output as the filename.
解决方案The docs on file uploads for gae are pretty vague. The only line in the webapp2 doc:
I'm going to follow this post with my own question on the subject, but if you use:
test = self.request.POST['filecsv'].value
instead of
getvalue()
(which throws an error for me), then you will see the rendered output of your csv file.这篇关于Google App Engine上传到TextArea(没有blobstore)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!