Editor.md解决跨域上传的问题

Editor.md解决跨域上传的问题

编辑 editormd\plugins\image-dialog\image-dialog.js

替换以下代码片段

if (settings.crossDomainUpload) {
action += "&callback=" + settings.uploadCallbackURL + "&dialog_id=editormd-image-dialog-" + guid;
}
var csrfToken = $('meta[name="csrf-token"]').attr('content');
var csrfField = "";
if (csrfToken) {
csrfField = "";
}
var dialogContent = ((settings.imageUpload) ? "<form action="" + action + "" target="" + iframeName + "" method="post" enctype="multipart/form-data" class="" + classPrefix + "form">" : "<div class="" + classPrefix + "form">") +
((settings.imageUpload) ? "<iframe name="" + iframeName + "" id="" + iframeName + "" guid="" + guid + "">" : "") +
"" +
"<input type="text" data-url />" + (function () {
return (settings.imageUpload) ? "<div class="" + classPrefix + "file-input">" +
"<input type="file" name="" + classPrefix + "image-file" accept="image/*" />" +
csrfField +
"<input type="submit" value="" + imageLang.uploadButton + "" />" +
"" : "";
})() +
"
" +
"" +
"<input type="text" value="" + selection + "" data-alt />" +
"
" +
"" +
"<input type="text" value="http://" data-link />" +
"
" + csrfField +
((settings.imageUpload) ? "" : "");

误区分析

代码第4行,name和自己设置的mete里的name保持一致
代码第7行,(flask框架下)name必须是csrf_token

上传图片部分代码

views.py

@post.route('/upload/', methods=['POST'])
def upload():
file = request.files.get('editormd-image-file')
if not file:
res = {
'success': 0,
'message': '上传失败'
}
else:
ex = os.path.splitext(file.filename)[1]
filename = datetime.now().strftime('%Y%m%d%H%M%S') + ex
path_uploads = config['uploads_path']
if not os.path.exists(path_uploads):
os.makedirs(path_uploads)
file.save(os.path.join(path_uploads, filename))
res = {
'success': 1,
'message': '上传成功',
'url': url_for('post.image', name=filename)
}
return jsonify(res)

@post.route('/image/')
def image(name):
path_uploads = config['uploads_path']
if not os.path.exists(path_uploads):
os.makedirs(path_uploads)

with open(os.path.join(path_uploads, name), 'rb') as f:
resp = Response(f.read(), mimetype="image/jpeg")
return resp

aaa.html





aaa.js

posted @ 2020-12-09 14:16  rm-rf*  阅读(358)  评论(0)    收藏  举报