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

Commit 57ebb542 by wyy

文本导出优化

parent 78bb1298
......@@ -131,6 +131,7 @@ export default {
templateId: this.piece.template, // TODO 模板id
annotatorId: this.$store.state.userInfo.userId,
creatorId: this.task.creator_id,
relationId: this.relationId,
textUrl: this.file.url,
type: "text",
fileId: this.file.id
......
......@@ -122,6 +122,7 @@ export default {
templateId: this.piece.template, // TODO 模板id
annotatorId: this.$store.state.userInfo.userId,
creatorId: this.task.creator_id,
relationId: this.relationId,
textUrl: this.file.url,
type: "text",
fileId: this.file.id
......
......@@ -107,6 +107,7 @@ export default {
templateId: this.piece.template, // TODO 模板id
annotatorId: this.$store.state.userInfo.userId,
creatorId: this.task.creator_id,
relationId: this.relationId,
textUrl: this.file.url,
type: "text",
fileId: this.file.id
......
......@@ -178,6 +178,7 @@ export default {
templateId: this.piece.template, // TODO 模板id
annotatorId: this.$store.state.userInfo.userId,
creatorId: this.task.creator_id,
relationId: this.relationId,
textUrl: this.file.url,
type: "xlsx",
fileId: this.file.id
......
......@@ -166,6 +166,7 @@ export default {
templateId: this.piece.template, // TODO 模板id
annotatorId: this.$store.state.userInfo.userId,
creatorId: this.task.creator_id,
relationId: this.relationId,
textUrl: this.file.url,
type: "xlsx",
fileId: this.file.id
......
......@@ -178,6 +178,7 @@ export default {
templateId: this.piece.template, // TODO 模板id
annotatorId: this.$store.state.userInfo.userId,
creatorId: this.task.creator_id,
relationId: this.relationId,
textUrl: this.file.url,
type: "xlsx",
fileId: this.file.id
......
......@@ -181,17 +181,18 @@ def insertDocument():
templateId = get_Data.get('templateId', None)
creatorId = get_Data.get('creatorId', None)
annotatorId = get_Data.get('annotatorId', None)
relationId = get_Data.get('relationId', None)
fileId = get_Data.get('fileId', None)
textUrl = get_Data.get('textUrl', None)
type = get_Data.get('type', None)
if templateId is None or creatorId is None or annotatorId is None or fileId is None or textUrl is None or type is None:
if templateId is None or creatorId is None or annotatorId is None or relationId is None or fileId is None or textUrl is None or type is None:
return_dict['return_code'] = '201'
return_dict['return_info'] = '请求参数有误'
return json.dumps(return_dict, ensure_ascii=False)
result = mongodbApi.findOne(collectionTest, {"templateId": templateId, "creatorId": creatorId, "annotatorId": annotatorId, "fileId": fileId,
"textUrl": textUrl, "type": type})
result = mongodbApi.findOne(collectionTest, {"templateId": templateId, "creatorId": creatorId, "annotatorId": annotatorId,
"relationId": relationId, "fileId": fileId, "textUrl": textUrl, "type": type})
if result is not None:
return_dict['result'] = result.get("documentId", None)
......@@ -200,7 +201,7 @@ def insertDocument():
documentId = str(uuid.uuid1()).replace('-', '')
result = mongodbApi.insert(collectionTest, {"documentId": documentId, "templateId": templateId, "creatorId": creatorId,
"annotatorId": annotatorId, "fileId": fileId, "textUrl": textUrl, "state": "0",
"annotatorId": annotatorId, "relationId": relationId, "fileId": fileId, "textUrl": textUrl, "state": "0",
"type": type, "tokenList": []})
if result is None:
......
......@@ -7,9 +7,11 @@ import PIL.Image
from datetime import datetime
import xlrd
import zipfile
from flask import Blueprint
from xlrd import xldate_as_tuple
from werkzeug.utils import secure_filename
from dao import mongodbApi
from support import basedir
......@@ -19,6 +21,7 @@ from views.textAnnotation import collectionTest
uploadDownload = Blueprint("uploadDownload", __name__, url_prefix="/api/files")
UPLOAD_FOLDER = 'upload' # 用于保存上传文件的文件夹名称
ZIP_FOLDER = 'zip' # 用于保存zip文件的文件夹名称
ALLOWED_EXTENSIONS = {'txt', 'xls', 'xlsx', 'png', 'jpg', 'bmp', 'gif'} # 允许上传的文件格式
......@@ -268,3 +271,55 @@ def downloadContent():
file_dir = os.path.join(basedir, UPLOAD_FOLDER, url)
return send_file(file_dir, as_attachment=True, attachment_filename=url)
# 打包下载
@uploadDownload.route('/downloadFile', methods=['POST'])
def downloadFile():
get_Data = request.get_data()
get_Data = json.loads(get_Data)
relationId = get_Data.get('relationId')
result = mongodbApi.findOne(collectionTest, {"relationId": relationId})
url = result.get('textUrl')
file_dir = os.path.join(basedir, UPLOAD_FOLDER, url)
return send_file(file_dir, as_attachment=True, attachment_filename=url)
# 下载压缩包接口
@uploadDownload.route('/downloadZip', methods=['POST'])
def downloadZip():
get_Data = request.get_data()
get_Data = json.loads(get_Data)
relationId = get_Data.get('relationId')
result = mongodbApi.findOne(collectionTest, {"relationId": relationId})
url = result.get('textUrl')
file_dir = os.path.join(basedir, UPLOAD_FOLDER) # 被压缩的文件所在文件夹路径 如 D:\learn\python-project\demo\upload
zip_dir = os.path.join(basedir, ZIP_FOLDER) # 压缩后的文件所在文件夹路径 如 D:\learn\python-project\demo\zip
portion = os.path.splitext(url) # 获取文件前缀名 如1590310217.txt portion[0]则为1590310217
new_name = portion[0] + '.zip' # 新的文件名字 如1590310217.zip
zipname = os.path.join(zip_dir, new_name) # 压缩的文件夹名字及路径 如 D:\learn\python-project\demo\zip\1590310217.zip (这是我电脑上的路径)
f = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED) #创建压缩文件 'w':写模式 ZIP_DEFLATED:设置压缩格式
basename = url #压缩后文件的根目录叫啥
fpath = os.path.join(file_dir, url) # fpath就是被压缩的文件的路径的集合 file_dir代表被压缩的文件的路径 name是被压缩的文件的名字
arcname = os.path.join(basename, url) # 这个不用管 是为了让压缩后的文件的根目录只有一层 不然会套娃n个文件夹(一层路径一个文件夹)
f.write(fpath, arcname=arcname) #写进去 压缩!
json_name = portion[0] + 'result.json' # 新的文件名字 如1590310217result.txt
full_path = os.path.join(file_dir, json_name) # 标注结果文件保存位置 + 标注结果文件名
file = open(full_path, 'w', encoding='utf8') #打开文件
file.write(str(result.get('tokenList'))) #写入标注结果字符串
file.close() #关闭文件
fpath = os.path.join(file_dir, json_name) # fpath就是被压缩的文件的路径的集合 file_dir代表被压缩的文件的路径 name是被压缩的文件的名字
arcname = os.path.join(basename, json_name) # 这个不用管 是为了让压缩后的文件的根目录只有一层 不然会套娃n个文件夹(一层路径一个文件夹)
f.write(fpath, arcname=arcname) # 写进去 压缩!
f.close() #关闭
return send_file(zipname, as_attachment=True, attachment_filename=new_name)
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