简体   繁体   中英

Google App Engine Upload to TextArea (without the blobstore)

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:

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.

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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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