文档服务地址:http://47.92.0.57:3000/ 周报索引地址:http://47.92.0.57:3000/s/NruNXRYmV

Commit 17927c2d by convivae

增加显示图片的api

parent f39b3d49
import json import json
import os import os
import time import time
import traceback
from datetime import datetime from datetime import datetime
import xlrd import xlrd
...@@ -10,13 +11,13 @@ from xlrd import xldate_as_tuple ...@@ -10,13 +11,13 @@ from xlrd import xldate_as_tuple
from dao import mongodbApi from dao import mongodbApi
from support import basedir from support import basedir
from flask import jsonify, request, send_file from flask import jsonify, request, send_file, Response
from views.textAnnotation import collectionTest from views.textAnnotation import collectionTest
uploadDownload = Blueprint("uploadDownload", __name__, url_prefix="/api/files") uploadDownload = Blueprint("uploadDownload", __name__, url_prefix="/api/files")
UPLOAD_FOLDER = 'upload' # 用于保存上传文件的文件夹名称 UPLOAD_FOLDER = 'upload' # 用于保存上传文件的文件夹名称
ALLOWED_EXTENSIONS = {'txt', 'xls', 'xlsx', 'png', 'jpg', 'bmp', 'gif'} # 允许上传的文件格式 ALLOWED_EXTENSIONS = {'txt', 'xls', 'xlsx', 'png', 'jpg', 'bmp', 'gif'} # 允许上传的文件格式
# 用于判断文件后缀 # 用于判断文件后缀
...@@ -32,21 +33,23 @@ def upload(): ...@@ -32,21 +33,23 @@ def upload():
os.makedirs(file_dir) os.makedirs(file_dir)
f = request.files['file'] # 从表单的file字段获取文件,file为该表单的name值 f = request.files['file'] # 从表单的file字段获取文件,file为该表单的name值
if f and allowed_file(f.filename): # 判断是否是允许上传的文件类型 if f and allowed_file(f.filename): # 判断是否是允许上传的文件类型
ext = os.path.splitext(f.filename)[1]# 获取文件后缀 ext = os.path.splitext(f.filename)[1] # 获取文件后缀
unix_time = int(time.time()) unix_time = int(time.time())
new_filename = str(unix_time)+ext # 修改了上传的文件名 new_filename = str(unix_time) + ext # 修改了上传的文件名
f.save(os.path.join(file_dir, new_filename)) # 保存文件到upload目录 f.save(os.path.join(file_dir, new_filename)) # 保存文件到upload目录
return jsonify({"code": 1, "msg": "succeed ", "data": new_filename}) return jsonify({"code": 1, "msg": "succeed ", "data": new_filename})
else: else:
return jsonify({"code": 0, "errmsg": u"failed"}) return jsonify({"code": 0, "errmsg": u"failed"})
# 下载接口 # 下载接口
@uploadDownload.route('/download/<fileName>', methods=['GET']) @uploadDownload.route('/download/<fileName>', methods=['GET'])
def download(fileName): def download(fileName):
file_dir = os.path.join(basedir, UPLOAD_FOLDER, fileName) file_dir = os.path.join(basedir, UPLOAD_FOLDER, fileName)
return send_file(file_dir, as_attachment=True, attachment_filename=fileName) return send_file(file_dir, as_attachment=True, attachment_filename=fileName)
#删除文档
# 删除文档
@uploadDownload.route('/delFile', methods=['POST']) @uploadDownload.route('/delFile', methods=['POST'])
def delFile(): def delFile():
data = request.get_json(silent=True) data = request.get_json(silent=True)
...@@ -101,6 +104,28 @@ def read_excel(path): ...@@ -101,6 +104,28 @@ def read_excel(path):
return json.dumps(data, ensure_ascii=False) return json.dumps(data, ensure_ascii=False)
# 显示图片url
@uploadDownload.route('/getImage/<imageName>', methods=["GET"])
def get_image_file(imageName):
try:
file_dir = os.path.join(basedir, UPLOAD_FOLDER, imageName)
if not os.path.exists(file_dir):
return jsonify({"code": 0, "errmsg": u"the image '{}' doesn't exit!".format(imageName)})
mdict = {
'jpeg': 'image/jpeg',
'jpg': 'image/jpeg',
'png': 'image/png',
'gif': 'image/gif'
}
mine = mdict[imageName.split('.')[1]]
with open(file_dir, 'rb') as f:
image = f.read()
return Response(image, mimetype=mine)
except Exception as e:
traceback.print_exc()
return jsonify({"code": 0, "errmsg": repr(e)})
# documentId获取文本内容 # documentId获取文本内容
@uploadDownload.route('/getFileContent', methods=['POST']) @uploadDownload.route('/getFileContent', methods=['POST'])
def getFileContent(): def getFileContent():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment