一、直接上传
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form method="post" action="/form/" enctype="multipart/form-data"> {% raw xsrf_form_html() %} 名称:<input type="text" name="name"><br> 爱好:<input type="checkbox" name="favor" value="1">水电费 <input type="checkbox" name="favor" value="2">的非官方 <input type="checkbox" name="favor" value="3">发过火 <input type="checkbox" name="favor" value="4">各环节<br> 头像:<input type="file" name="pic"><br> <input type="submit" value="提交"> </form> </body> </html>
服务端:
1 class FormHandler(BaseHandler): 2 def get(self, *args, **kwargs): 3 self.render(‘form.html‘) 4 5 def post(self, *args, **kwargs): 6 name = self.get_argument(‘name‘) 7 favor = self.get_arguments(‘favor‘) 8 pics = self.request.files[‘pic‘] 9 print(‘name=‘,name) 10 print(‘favor=‘,favor) 11 # print(‘pic=‘,pic) 12 for pic in pics: 13 pic_name = pic[‘filename‘] 14 with open(os.path.join(‘static‘,‘img‘,pic_name),‘wb‘) as up: 15 up.write(pic[‘body‘]) 16 self.redirect(‘/form/‘)
时间: 2024-11-08 21:11:26