def post(self, userID): """ 上传头像 :return: """ from werkzeug.utils import secure_filename from faker import Faker f = Faker() file = request.files.get('file') if not file: return jsonify(myResponse(ResponseCode.ERROR, None, ResponseError.NO_FIlE)) if not userID: return jsonify(myResponse(ResponseCode.ERROR, None, cantEmpty("userID"))) user = User.get(userID) fileName = secure_filename(file.filename) filePath = getAvatarPath(f.pystr() + '_' + fileName) user.avatar = filePath try: file.save(filePath) user.save() return jsonify(myResponse(ResponseCode.SUCCESS, user.id, ResponseError.OK)) except Exception as e: log.error(e) return jsonify(myResponse(ResponseCode.ERROR, None, ResponseCode.ERROR_TRY_AGAIN))
def get(self, userID): user = User.get(userID) avatar = user.avatar with open(avatar, "rb") as f: avatar = f.read() return Response(avatar, mimetype="image/jpeg")