0
votes

I am unable to figure out what I am doing wrong while uploading file using web form (python, webapp2, gae) and writing to cloud storage (GCS)

jinja2 template

<form action="/upload_data" method="post">
<input type="file" name="add_scanned_data_file" enctype="multipart/form-data">
<input type="image" src="stylesheets/add_data.png" alt="submit" align="left"></form>

webapp2 code

fx = self.request.body_file
fx_name = "some_name"
gcs_file = gcs.open(_GCS_BUCKET_NAME + fx_name, 'w')
gcs_file.write(fx.read())
gcs_file.close()

The file gets uploaded to GCS but incorrectly. The content of uploaded file in GCS shows only the file name with some form parameter string. So it appears that I am not reading input file from webform properly.

_________Edit (Text added below)___________

While this (adding multipart in form tag) works so far and (gcs_file.write(fx.read()) uploads file in GCS. However when I try to read that file (either from GCS Browser or by self.response.write(gcs_file.read()) ), I get output as below (which means I need to do something with file metadata??)

                                                  ------WebKitFormBoundaryk9MfD0ETQjUVirBO Content-Disposition: form-data; name="add_scanned_data_update_date" ------WebKitFormBoundaryk9MfD0ETQjUVirBO Content-Disposition: form-data; name="add_scanned_data_text" Upload ------WebKitFormBoundaryk9MfD0ETQjUVirBO Content-Disposition: form-data; name="add_scanned_data_file"; filename="sample.jpg" Content-type: image/jpeg ����JFIF,,��xPhotoshop 3.08BIM\gP53616c7465645f5f605b5cccc1a3ffb06bd320d5ad9ccaa25a68c613f04d7ad0dcaed6e1c049c109��C��C
1
FYI you can also upload directly to cloud storage using create_upload_url functionmarcadian
Yes, though I am just now struggling to understand if gcs.write() actually first writes metadata and then binary file? Or gcs.read() reads metadata+binary, and some post-processing is required before it is shown as "Save File As" to the end user...=Nick
gcs is file like interface in python. It writes what you write into it, it does not know metadata or binary or whatever.marcadian
Yes. I have opened a new question stackoverflow.com/questions/30172782 as essentially i am now struggling to split the multiform data.Nick

1 Answers

3
votes

enctype is an attribute of the form tag:

<form action="/upload_data" method="post" enctype="multipart/form-data">
<input type="file" name="add_scanned_data_file">
<input type="image" src="stylesheets/add_data.png" alt="submit" align="left">
</form>