简体   繁体   English

Google App Engine上传到TextArea(无Blobstore)

[英]Google App Engine Upload to TextArea (without the blobstore)

I'm trying to get simple file reading in GAE python. 我正在尝试在GAE python中读取简单的文件。 I want to allow users to upload a csv file directly from their disk and have it display in a text field. 我想允许用户直接从其磁盘上载一个csv文件,并将其显示在文本字段中。 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. 如果可能,我宁愿避免使用Blobstore。 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. 现在,textarea将作为文件名输出。

The docs on file uploads for gae are pretty vague. 关于gae的文件上传文档非常模糊。 The only line in the webapp2 doc: webapp2文档中的唯一一行:

Uploaded files are available as cgi.FieldStorage (see the cgi module) instances directly in request.POST. 上传的文件可以直接在request.POST中作为cgi.FieldStorage(请参阅cgi模块)实例使用。

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. 而不是getvalue() (这对我抛出错误),然后您将看到csv文件的渲染输出。

the easiest way is to get directly from the request object 最简单的方法就是get直接从请求对象

self.request.get("filecsv")

this will give you the csv file (in form of raw data) 这将为您提供csv文件(以原始数据的形式)

Also, you probably shouldn't generate html from inside the post method. 另外,您可能不应该从post方法内部生成html。 After doing this and the client wants to refresh the page s/he will be asked whether to resend the post data to which they might innocently click yes. 完成此操作后,客户想刷新页面,系统将询问他/他是否重新发送帖子数据,他们可能会无辜地单击“是”。 Best thing to do is to redirect to another or the same url 最好的办法是重定向到另一个或相同的URL

self.redirect("/another-page")

or if you'd like to go back to the same page 或者如果您想返回同一页面

self.redirect(self.request.url) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM