加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
run.py 2.26 KB
一键复制 编辑 原始数据 按行查看 历史
wq599263163 提交于 2016-12-26 00:14 . Upload run.py
# -*- coding: utf-8 -*-
import os
import re
import json
import random
import urllib
import datetime
from main.form import BlogForms
from flask import Flask, request, render_template, url_for, make_response
app = Flask(__name__, static_url_path='')
app.secret_key = 'fdsjdsafjlkfdsajlk'
app.debug = True
def gen_rnd_filename():
filename_prefix = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
return '%s%s' % (filename_prefix, str(random.randrange(1000, 10000)))
@app.route('/')
def index():
form = BlogForms()
return render_template('index.html', form=form)
@app.route('/blog-form/', methods=['POST'])
def blog_form():
"""接收提交过来表单数据"""
form = BlogForms(request.form)
form = form.body.data
return form
@app.route('/ckupload/', methods=['POST', 'OPTIONS'])
def ckupload():
"""CKEditor file upload
文件上传,
"""
error = ''
url = ''
callback = request.args.get("CKEditorFuncNum")
if request.method == 'POST' and 'upload' in request.files:
fileobj = request.files['upload']
fname, fext = os.path.splitext(fileobj.filename)
# rnd_name = '%s%s' % (gen_rnd_filename(), fext) # python2 老代码
rnd_name = '{}{}'.format(gen_rnd_filename(), fext)
filepath = os.path.join(app.static_folder, 'upload', rnd_name)
# 检查路径是否存在,不存在则创建
dirname = os.path.dirname(filepath)
if not os.path.exists(dirname):
try:
os.makedirs(dirname)
except:
error = 'ERROR_CREATE_DIR'
elif not os.access(dirname, os.W_OK):
error = 'ERROR_DIR_NOT_WRITEABLE'
if not error:
fileobj.save(filepath)
# url = url_for('static', filename='%s/%s' % ('upload', rnd_name))
url = url_for('static', filename='{}/{}'.format('upload', rnd_name))
else:
error = 'post error'
res = """<script type="text/javascript">
window.parent.CKEDITOR.tools.callFunction(%s, '%s', '%s');
</script>""" % (callback, url, error)
response = make_response(res)
response.headers["Content-Type"] = "text/html"
return response
if __name__ == '__main__':
app.run()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化