I am using a form to upload files to the google app engine and store them in the datastore. l would also like to store the original file name and extension for presentation purposes.
Is there a way of retrieving this data from the post sever side or can it only be gathered client side and sent as separate fields (eg http://www.tinyurl.com/5jybfq )?
Many thanks.
For those that follow the solution dased on the answer below:
this_file = self.request.params["file_upload_form_field_name"].filename
(this_file_name, this_file_extension) = os.path.splitext(this_file)
self.response.out.write("File name: " + this_file_name + "<br>")
self.response.out.write("File extension: " + this_file_extension + "<br>")
此电子邮件中的代码段是否有效?
self.request.params[<form element name with file>].filename
Try this...
If your form looks like this:
self.response.out.write('''<form action="/upload" enctype="multipart/form-data" method="post"><input type="file" name="file"/><input type="submit" /></form>''')
Then in your post handler, try this:
class UploadHandler(webapp.RequestHandler):
def post(self):
obj = MyModel()
obj.filename = self.request.get("file")
obj.blob = self.request.get("contents")
obj.put()
self.redirect('/')
The filename is stored in the "file" parameter. The actual file contents are in the "contents" parameter.
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.