[英]Getting the name & extension of an uploaded file using python (google app engine)
I am using a form to upload files to the google app engine and store them in the datastore. 我正在使用表单将文件上传到Google应用引擎并将其存储在数据存储区中。 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 )? 有没有办法从后服务器端检索这些数据,或者它只能被客户端收集并作为单独的字段发送(例如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. 文件名存储在“file”参数中。 The actual file contents are in the "contents" parameter.
实际文件内容位于“contents”参数中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.