def uploadImage(): resp = {'state': 'SUCCESS', 'url': '', 'title': '', 'original': ''} file_target = request.files upfile = file_target['upfile'] if 'upfile' in file_target else None if upfile is None: resp['state'] = "上传失败" return jsonify(resp) ret = UploadService.uploadByFile(upfile) if ret['code'] != 200: #根据UploadService.uploadByFile方法里面返回的code值进行判断,如果不是200就说明这个文件类型不合格 resp['state'] = "上传失败:" + ret['msg'] return jsonify(resp) #如果没有出现失败的情况,就说明 成功了,然后点返回一个图片上传存放的地址回去 resp['url'] = UrlManager.buildImageUrl( ret['data']['file_key']) #这里在UrlManager.buildImageUrl进行了图片展示地址的拼接 return jsonify(resp)
def listImage(): #从数据库里面取数据进行 展示,服务于在线管理那个里面,里面的start和id的结合是为了进行图片页面分类 resp = {'state': 'SUCCESS', 'list': [], 'start': 0, 'total': 0} req = request.values start = int(req['start']) if 'start' in req else 0 page_size = int(req['size']) if 'size' in req else 20 query = Image.query if start > 0: query = query.filter(Image.id < start) list = query.order_by(Image.id.desc()).limit(page_size).all() images = [] if list: for item in list: #取出数据,是将数据库中的地址挨个放进一个列表里面 images.append({'url': UrlManager.buildImageUrl(item.file_key)}) start = item.id resp['list'] = images resp['start'] = start resp['total'] = len(images) return jsonify(resp)