Skip to content

Commit

Permalink
为simditor增加上传功能
Browse files Browse the repository at this point in the history
  • Loading branch information
时雨 committed May 10, 2015
1 parent 8793c20 commit 98fb06f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
16 changes: 9 additions & 7 deletions controller/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,13 @@ def check_xsrf_cookie(self):
@tornado.web.asynchronous
@gen.coroutine
def post(self, *args, **kwargs):
self.func = intval(self.get_argument("CKEditorFuncNum"))
now = time.time()
try:
if self.request.files:
file = self.request.files['upload'][0]
ext = os.path.splitext(file["filename"])[-1]
if ext not in (".png", ".gif", ".jpg", ".bmp", ".jpeg"):
self.end("", u"不允许上传此类后缀的文件哦")
self.end(False, u"不允许上传此类后缀的文件哦")
self.orgname = file["filename"]
filename = md5("%s%s" % (file["filename"], random_str(6))) + ext
folder = "%s/%s/%s" % (self.settings["imagepath"], humantime(now, "%Y%m"),
Expand All @@ -201,15 +200,18 @@ def post(self, *args, **kwargs):
filename = "%s/%s" % (folder, filename)
with open(filename, "wb") as fin:
fin.write(file["body"])
fin.close()
self.end(filename, u"上传成功")
self.end(True, u"上传成功", filename)
except tornado.web.Finish, e:
pass
except:
import traceback
print traceback.print_exc()
self.end("", u"参数错误")
self.end(False, u"参数错误")

def end(self, path, info):
self.render("upload.htm", func = self.func, orgname = self.orgname, path = path, info = info)
def end(self, status, info, path = ""):
self.write({
"success": status,
"msg": info,
"file_path": path
})
raise tornado.web.Finish()
7 changes: 6 additions & 1 deletion static/assets/js/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ $(document).ready(function(){
'|',
'source',
],

upload: {
url: "/uploader",
fileKey: "upload",
connectionCount: 1,
leaveConfirm: '正在上传文件,如果离开上传会自动取消'
}
});

});
7 changes: 6 additions & 1 deletion static/assets/js/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ $(document).ready(function(){
'|',
'source',
],

upload: {
url: "/uploader",
fileKey: "upload",
connectionCount: 1,
leaveConfirm: '正在上传文件,如果离开上传会自动取消'
}
});
$("#publish").click(function(){
var title = $("[name='title']").val();
Expand Down
2 changes: 1 addition & 1 deletion templates/edit.htm
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@
<script src="{{ static_url('assets/simditor/scripts/hotkeys.js') }}"></script>
<script src="{{ static_url('assets/simditor/scripts/uploader.js') }}"></script>
<script src="{{ static_url('assets/simditor/scripts/simditor.js') }}"></script>
<script src="{{ static_url('assets/js/publish.js') }}"></script>
<script src="{{ static_url('assets/js/edit.js') }}"></script>
<script src="{{ static_url('assets/js/app.js') }}"></script>
{% end %}
4 changes: 2 additions & 2 deletions templates/open_post.htm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{% block header_title %}{{ post["title"] }}{% end %}
{% block header_static %}
<link rel="stylesheet" href="{{ static_url('assets/ckeditor/plugins/codesnippet/lib/highlight/styles/default.css') }}">
<link rel="stylesheet" href="{{ static_url('assets/highlightjs/default.min.css') }}">
{% end %}
{% block body %}
{% include "header.htm" %}
Expand Down Expand Up @@ -132,7 +132,7 @@

{% end %}
{% block footer_static %}
<script src="{{ static_url('assets/ckeditor/plugins/codesnippet/lib/highlight/highlight.pack.js') }}"></script>
<script src="{{ static_url('assets/highlightjs/highlight.min.js') }}"></script>
<script src="{{ static_url('assets/js/app.js') }}"></script>
<script src="{{ static_url('assets/js/post.js') }}"></script>
<script src="{{ static_url('assets/js/nologin.js') }}"></script>
Expand Down

0 comments on commit 98fb06f

Please sign in to comment.