Reputation: 1819
I am following Massimiliano Pippi's Python for Google App engine. In chapter 3, we are trying to upload a file that the user of my app select thanks to this html code:
<div class="form-group">
<label for="uploaded_file">Attached file:</label>
<input type="file" id="uploaded_file" name="uploaded_file">
</div>
And from the python part, in my MainHandler
on webapp2, I get the request content using:
def post(self):
uploaded_file = self.request.POST.get("uploaded_file", None)
file_name = getattr(uploaded_file, 'filename')
file_content = getattr(uploaded_file, 'file', None)
content_t = mimetypes.guess_type(file_name)[0]
bucket_name = app_identity.get_default_gcs_bucket_name()
path = os.path.join('/', bucket_name, file_name)
with cloudstorage.open(path, 'w', content_type=content_t) as f:
f.write(file_content.read())
The problem is that the variable uploaded_file
is handled as if it was a file by Massimiliano Pippi, but my Python tells me that this variable is a unicode containing the name of the file. Therefore, when I try file_name = getattr(uploaded_file, 'filename')
, I get an error.
Obviously, the code in the book is false, how can I fix it?
Upvotes: 0
Views: 123
Reputation: 1819
Ok so after Tim's advice, I chcked the doc of WebOb and notice that in the html file, I should have put enctype="multipart/form-data"
as follows:
<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="uploaded_file">Attached file:</label>
<input type="file" id="uploaded_file" name="uploaded_file">
</div>
</form>
Instead of:
<form action="" method="post">
<div class="form-group">
<label for="uploaded_file">Attached file:</label>
<input type="file" id="uploaded_file" name="uploaded_file">
</div>
</form>
Upvotes: 1