def post(self): title = self.get_argument("title", "") content = self.get_argument("content", "") upload_path = "statics/upload" # 配置上传的路径 file_metas = self.request.files.get("image_file", []) # 获取图片 # 写入文件 for meta in file_metas: image_type = meta.get("filename").split(".")[-1] # 获取后缀名 file_name = str(uuid.uuid1()) + "." + image_type # 构造文件名 file_path = os.path.join(upload_path, file_name) with open(file_path, "wb") as up: up.write(meta.get("body")) # 写入内容 # 缩略图 im = Image.open(file_path) # 打开图片 im.thumbnail((259.69, 270)) thumbnail_url = "upload/thumbnail/%s" % file_name im.save("statics/%s" % thumbnail_url, image_type if image_type == "png" else "JPEG") # 入库 Post.add_post(title, content, "upload/%s" % file_name, thumbnail_url, self.current_user, 2) return self.write("上传成功")
def post(self): title = self.get_argument("title", "") #获取前端输入的值 content = self.get_argument("content", "") upload_path = "static/upload" #配置上传路径 file_metas = self.request.files.get("image_file", []) #获取上传图片 #写入文件 for meta in file_metas: image_type = meta.get("filename").split(".")[-1] #获取后缀名 file_name = str(uuid.uuid1()) + "." + image_type #构造文件名 file_path = os.path.join(upload_path, file_name) #拼接上传路径 with open(file_path, "wb") as up: up.write(meta.get("body")) #写入文件内容 #缩略图 im = Image.open(file_path) #打开图片 im.thumbnail((256.69, 270)) im.save(file_path, image_type if image_type == "png" else "JPEG") thumbnail_url = "upload/thumbnail/{}".format(file_name) #缩略图保存地址 #入库 # print(self.current_user) #获取当前用户用户名 Post.add_post(title, content, "upload/{}".format(file_name), thumbnail_url, self.current_user, 3) return self.write("上传成功")
def post(self): upload_path = "statics/upload" # 配置上传的路径 file_metas = self.request.files.get("image_file", []) # 获取图片 # 写入文件 for meta in file_metas: file_name = meta.get("filename") file_path = os.path.join(upload_path, file_name) with open(file_path, "wb") as up: up.write(meta.get("body")) # 写入内容 # 入库 Post.add_post("upload/%s" % file_name, self.current_user) return self.write("上传成功")
def add_post(self, username, image_url, thumb_url): user = self.session.query(User).filter_by(username=username).first() post = Post(user_id=user.id, image_url=image_url, thumb_url=thumb_url) self.session.add(post) self.session.commit() post_id = post.user_id return post_id
def add_post(self, img_url, thumb_url, username): user = self.get_user(username) post = Post(image_url=img_url, thumb_url=thumb_url, user=user) self.db.add(post) self.db.commit() post_id = post.id return post_id
def add_post(self, image_url, thumb_url, username): user = self.db.query(User).filter_by(username=username).first() # session.add(Post(image_url=image_url, user=user)) post = Post(image_url=image_url, thumb_url=thumb_url, user=user) self.db.add(post) self.db.commit() post_id = post.id self.db.close() return post_id
def add_post(img_url, thumb_url, username): session = Session() user = session.query(User).filter_by(username=username).first() post = Post(image_url=img_url, thumb_url=thumb_url, user_id=user.id) session.add(post) session.commit() post_id = post.id session.close() return post_id
def add_post(self, image_url, thumb_url, username): """ 上传图片保存到数据库 :param image_url: :param username: :return: """ user = self.get_user(username) post = Post(image_url=image_url, thumb_url=thumb_url, user=user) self.db_session.add(post) self.db_session.commit() post_id = post.id return post_id