Google App Engine Upload to TextArea (without the blobstore)

杀马特。学长 韩版系。学妹 提交于 2020-01-06 02:14:07

问题


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.


回答1:


The docs on file uploads for gae are pretty vague. The only line in the webapp2 doc:

Uploaded files are available as cgi.FieldStorage (see the cgi module) instances directly in request.POST.

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.




回答2:


the easiest way is to get directly from the request object

self.request.get("filecsv")

this will give you the csv file (in form of raw data)

Also, you probably shouldn't generate html from inside the post method. 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

self.redirect("/another-page")

or if you'd like to go back to the same page

self.redirect(self.request.url) 


来源:https://stackoverflow.com/questions/17226207/google-app-engine-upload-to-textarea-without-the-blobstore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!