fix:1、修复附件无法同时上传多个的BUG

2、新增客户端校验附件大小
This commit is contained in:
lifei6671
2018-09-11 15:57:03 +08:00
parent 929f54840b
commit 90efeac326
8 changed files with 59 additions and 57 deletions
+3
View File
@@ -186,6 +186,9 @@ func IsAllowUploadFileExt(ext string) bool {
exts := GetUploadFileExt() exts := GetUploadFileExt()
for _, item := range exts { for _, item := range exts {
if item == "*" {
return true
}
if strings.EqualFold(item, ext) { if strings.EqualFold(item, ext) {
return true return true
} }
+6 -3
View File
@@ -12,6 +12,8 @@ import (
"github.com/lifei6671/mindoc/conf" "github.com/lifei6671/mindoc/conf"
"github.com/lifei6671/mindoc/models" "github.com/lifei6671/mindoc/models"
"github.com/lifei6671/mindoc/utils" "github.com/lifei6671/mindoc/utils"
"github.com/lifei6671/mindoc/utils/filetil"
"path/filepath"
) )
type BaseController struct { type BaseController struct {
@@ -56,9 +58,6 @@ func (c *BaseController) Prepare() {
} }
} }
} }
//c.Member = models.NewMember()
//c.Member.Find(1)
//c.Data["Member"] = *c.Member
} }
conf.BaseUrl = c.BaseUrl() conf.BaseUrl = c.BaseUrl()
c.Data["BaseUrl"] = c.BaseUrl() c.Data["BaseUrl"] = c.BaseUrl()
@@ -78,6 +77,10 @@ func (c *BaseController) Prepare() {
} }
} }
c.Data["HighlightStyle"] = beego.AppConfig.DefaultString("highlight_style","github") c.Data["HighlightStyle"] = beego.AppConfig.DefaultString("highlight_style","github")
if filetil.FileExists(filepath.Join(beego.BConfig.WebConfig.ViewsPath,"widgets","scripts.tpl")) {
c.LayoutSections["Scripts"] = "widgets/scripts.tpl"
}
} }
// SetMember 获取或设置当前登录用户信息,如果 MemberId 小于 0 则标识删除 Session // SetMember 获取或设置当前登录用户信息,如果 MemberId 小于 0 则标识删除 Session
+5 -2
View File
@@ -337,7 +337,6 @@ func (c *BlogController) ManageEdit() {
blog.ModifyAt = c.Member.MemberId blog.ModifyAt = c.Member.MemberId
blog.Modified = time.Now() blog.Modified = time.Now()
if err := blog.Save("blog_content","blog_release","modify_at","modify_time","version");err != nil { if err := blog.Save("blog_content","blog_release","modify_at","modify_time","version");err != nil {
beego.Error("保存文章失败 -> ",err) beego.Error("保存文章失败 -> ",err)
c.JsonResult(6011,"保存文章失败") c.JsonResult(6011,"保存文章失败")
@@ -374,7 +373,11 @@ func (c *BlogController) ManageEdit() {
}else{ }else{
c.Data["AttachList"] = template.JS("[]") c.Data["AttachList"] = template.JS("[]")
} }
if conf.GetUploadFileSize() > 0 {
c.Data["UploadFileSize"] = conf.GetUploadFileSize()
}else{
c.Data["UploadFileSize"] = "undefined";
}
c.Data["Model"] = blog c.Data["Model"] = blog
} }
+15 -11
View File
@@ -186,7 +186,7 @@ func (c *DocumentController) Read() {
Body string `json:"body"` Body string `json:"body"`
Title string `json:"title"` Title string `json:"title"`
DocInfo string `json:"doc_info"` DocInfo string `json:"doc_info"`
Version int64 `json:"version"` Version int64 `json:"version"`
} }
data.DocTitle = doc.DocumentName data.DocTitle = doc.DocumentName
data.Body = doc.Release data.Body = doc.Release
@@ -282,6 +282,12 @@ func (c *DocumentController) Edit() {
} }
c.Data["BaiDuMapKey"] = beego.AppConfig.DefaultString("baidumapkey", "") c.Data["BaiDuMapKey"] = beego.AppConfig.DefaultString("baidumapkey", "")
if conf.GetUploadFileSize() > 0 {
c.Data["UploadFileSize"] = conf.GetUploadFileSize()
}else{
c.Data["UploadFileSize"] = "undefined";
}
} }
// 创建一个文档 // 创建一个文档
@@ -291,7 +297,7 @@ func (c *DocumentController) Create() {
docName := c.GetString("doc_name") docName := c.GetString("doc_name")
parentId, _ := c.GetInt("parent_id", 0) parentId, _ := c.GetInt("parent_id", 0)
docId, _ := c.GetInt("doc_id", 0) docId, _ := c.GetInt("doc_id", 0)
isOpen,_ := c.GetInt("is_open",0) isOpen, _ := c.GetInt("is_open", 0)
if identify == "" { if identify == "" {
c.JsonResult(6001, "参数错误") c.JsonResult(6001, "参数错误")
@@ -353,7 +359,7 @@ func (c *DocumentController) Create() {
if isOpen == 1 { if isOpen == 1 {
document.IsOpen = 1 document.IsOpen = 1
}else{ } else {
document.IsOpen = 0 document.IsOpen = 0
} }
@@ -397,19 +403,17 @@ func (c *DocumentController) Upload() {
} }
if conf.GetUploadFileSize() > 0 && moreFile.Size > conf.GetUploadFileSize() { if conf.GetUploadFileSize() > 0 && moreFile.Size > conf.GetUploadFileSize() {
c.JsonResult(6009, "查过文件允许的上传最大值") c.JsonResult(6009, "文件大小超过了限定的最大值")
} }
ext := filepath.Ext(moreFile.Filename) ext := filepath.Ext(moreFile.Filename)
//文件必须带有后缀名
if ext == "" { if ext == "" {
c.JsonResult(6003, "无法解析文件的格式") c.JsonResult(6003, "无法解析文件的格式")
} }
//如果文件类型设置为 * 标识不限制文件类型 //如果文件类型设置为 * 标识不限制文件类型
if beego.AppConfig.DefaultString("upload_file_ext", "") != "*" { if conf.IsAllowUploadFileExt(ext) == false {
if !conf.IsAllowUploadFileExt(ext) { c.JsonResult(6004, "不允许的文件类型")
c.JsonResult(6004, "不允许的文件类型")
}
} }
bookId := 0 bookId := 0
@@ -504,7 +508,7 @@ func (c *DocumentController) Upload() {
if err != nil { if err != nil {
os.Remove(filePath) os.Remove(filePath)
beego.Error("Attachment Insert => ", err) beego.Error("文件保存失败 ->", err)
c.JsonResult(6006, "文件保存失败") c.JsonResult(6006, "文件保存失败")
} }
@@ -512,7 +516,7 @@ func (c *DocumentController) Upload() {
attachment.HttpPath = conf.URLFor("DocumentController.DownloadAttachment", ":key", identify, ":attach_id", attachment.AttachmentId) attachment.HttpPath = conf.URLFor("DocumentController.DownloadAttachment", ":key", identify, ":attach_id", attachment.AttachmentId)
if err := attachment.Update(); err != nil { if err := attachment.Update(); err != nil {
beego.Error("SaveToFile => ", err) beego.Error("保存文件失败 ->", err)
c.JsonResult(6005, "保存文件失败") c.JsonResult(6005, "保存文件失败")
} }
} }
+9 -14
View File
@@ -237,10 +237,8 @@
formData : { "blogId" : {{.Model.BlogId}}}, formData : { "blogId" : {{.Model.BlogId}}},
pick: "#filePicker", pick: "#filePicker",
fileVal : "editormd-file-file", fileVal : "editormd-file-file",
fileNumLimit : 1, fileSingleSizeLimit: {{.UploadFileSize}},
compress : false compress : false
}).on("beforeFileQueued",function (file) {
uploader.reset();
}).on( 'fileQueued', function( file ) { }).on( 'fileQueued', function( file ) {
var item = { var item = {
state : "wait", state : "wait",
@@ -249,15 +247,14 @@
file_name : file.name, file_name : file.name,
message : "正在上传" message : "正在上传"
}; };
window.vueApp.lists.splice(0,0,item); window.vueApp.lists.push(item);
}).on("uploadError",function (file,reason) { }).on("uploadError",function (file,reason) {
for(var i in window.vueApp.lists){ for(var i in window.vueApp.lists){
var item = window.vueApp.lists[i]; var item = window.vueApp.lists[i];
if(item.attachment_id == file.id){ if(item.attachment_id == file.id){
item.state = "error"; item.state = "error";
item.message = "上传失败"; item.message = "上传失败" + reason;
break;
} }
} }
@@ -267,25 +264,23 @@
var item = window.vueApp.lists[index]; var item = window.vueApp.lists[index];
if(item.attachment_id === file.id){ if(item.attachment_id === file.id){
if(res.errcode === 0) { if(res.errcode === 0) {
window.vueApp.lists.splice(index, 1, res.attach); window.vueApp.lists.splice(index, 1, res.attach?res.attach:res.data);
}else{ }else{
item.message = res.message; item.message = res.message;
item.state = "error"; item.state = "error";
} }
break;
} }
} }
}).on("beforeFileQueued",function (file) {
}).on("uploadComplete",function () {
}).on("uploadProgress",function (file, percentage) { }).on("uploadProgress",function (file, percentage) {
var $li = $( '#'+file.id ), var $li = $( '#'+file.id ),
$percent = $li.find('.progress .progress-bar'); $percent = $li.find('.progress .progress-bar');
$percent.css( 'width', percentage * 100 + '%' ); $percent.css( 'width', percentage * 100 + '%' );
}).on("error", function (type) {
if(type === "F_EXCEED_SIZE"){
layer.msg("文件超过了限定大小");
}
console.log(type);
}); });
}catch(e){ }catch(e){
console.log(e); console.log(e);
+1 -2
View File
@@ -278,7 +278,6 @@
<script src="{{cdnjs "/static/js/kancloud.js" "version"}}" type="text/javascript"></script> <script src="{{cdnjs "/static/js/kancloud.js" "version"}}" type="text/javascript"></script>
<script src="{{cdnjs "/static/js/splitbar.js" "version"}}" type="text/javascript"></script> <script src="{{cdnjs "/static/js/splitbar.js" "version"}}" type="text/javascript"></script>
<script type="text/javascript"> <script type="text/javascript">
$(function () { $(function () {
$("#searchList").on("click","a",function () { $("#searchList").on("click","a",function () {
var id = $(this).attr("data-id"); var id = $(this).attr("data-id");
@@ -290,7 +289,7 @@ $(function () {
}); });
}); });
}); });
</script> </script>
{{.Scripts}}
</body> </body>
</html> </html>
+11 -13
View File
@@ -427,10 +427,10 @@
formData : { "identify" : {{.Model.Identify}},"doc_id" : window.selectNode.id }, formData : { "identify" : {{.Model.Identify}},"doc_id" : window.selectNode.id },
pick: "#filePicker", pick: "#filePicker",
fileVal : "editormd-file-file", fileVal : "editormd-file-file",
fileNumLimit : 1, compress : false,
compress : false fileSingleSizeLimit: {{.UploadFileSize}}
}).on("beforeFileQueued",function (file) { }).on("beforeFileQueued",function (file) {
uploader.reset(); // uploader.reset();
this.options.formData.doc_id = window.selectNode.id; this.options.formData.doc_id = window.selectNode.id;
}).on( 'fileQueued', function( file ) { }).on( 'fileQueued', function( file ) {
var item = { var item = {
@@ -440,14 +440,14 @@
file_name : file.name, file_name : file.name,
message : "正在上传" message : "正在上传"
}; };
window.vueApp.lists.splice(0,0,item); window.vueApp.lists.push(item);
}).on("uploadError",function (file,reason) { }).on("uploadError",function (file,reason) {
for(var i in window.vueApp.lists){ for(var i in window.vueApp.lists){
var item = window.vueApp.lists[i]; var item = window.vueApp.lists[i];
if(item.attachment_id == file.id){ if(item.attachment_id == file.id){
item.state = "error"; item.state = "error";
item.message = "上传失败"; item.message = "上传失败:" + reason;
break; break;
} }
} }
@@ -458,25 +458,23 @@
var item = window.vueApp.lists[index]; var item = window.vueApp.lists[index];
if(item.attachment_id === file.id){ if(item.attachment_id === file.id){
if(res.errcode === 0) { if(res.errcode === 0) {
window.vueApp.lists.splice(index, 1, res.attach); window.vueApp.lists.splice(index, 1, res.attach ? res.attach : res.data);
}else{ }else{
item.message = res.message; item.message = res.message;
item.state = "error"; item.state = "error";
} }
break;
} }
} }
}).on("beforeFileQueued",function (file) {
}).on("uploadComplete",function () {
}).on("uploadProgress",function (file, percentage) { }).on("uploadProgress",function (file, percentage) {
var $li = $( '#'+file.id ), var $li = $( '#'+file.id ),
$percent = $li.find('.progress .progress-bar'); $percent = $li.find('.progress .progress-bar');
$percent.css( 'width', percentage * 100 + '%' ); $percent.css( 'width', percentage * 100 + '%' );
}).on("error", function (type) {
if(type === "F_EXCEED_SIZE"){
layer.msg("文件超过了限定大小");
}
console.log(type);
}); });
}catch(e){ }catch(e){
console.log(e); console.log(e);
+9 -12
View File
@@ -419,10 +419,9 @@
formData : { "identify" : {{.Model.Identify}},"doc_id" : window.selectNode.id }, formData : { "identify" : {{.Model.Identify}},"doc_id" : window.selectNode.id },
pick: "#filePicker", pick: "#filePicker",
fileVal : "editormd-file-file", fileVal : "editormd-file-file",
fileNumLimit : 1, compress : false,
compress : false fileSingleSizeLimit: {{.UploadFileSize}}
}).on("beforeFileQueued",function (file) { }).on("beforeFileQueued",function (file) {
uploader.reset();
this.options.formData.doc_id = window.selectNode.id; this.options.formData.doc_id = window.selectNode.id;
}).on( 'fileQueued', function( file ) { }).on( 'fileQueued', function( file ) {
var item = { var item = {
@@ -432,20 +431,18 @@
file_name : file.name, file_name : file.name,
message : "正在上传" message : "正在上传"
}; };
window.vueApp.lists.splice(0,0,item); window.vueApp.lists.push(item);
}).on("uploadError",function (file,reason) { }).on("uploadError",function (file,reason) {
for(var i in window.vueApp.lists){ for(var i in window.vueApp.lists){
var item = window.vueApp.lists[i]; var item = window.vueApp.lists[i];
if(item.attachment_id == file.id){ if(item.attachment_id == file.id){
item.state = "error"; item.state = "error";
item.message = "上传失败"; item.message = "上传失败:" + reason;
break;
} }
} }
}).on("uploadSuccess",function (file, res) { }).on("uploadSuccess",function (file, res) {
for(var index in window.vueApp.lists){ for(var index in window.vueApp.lists){
var item = window.vueApp.lists[index]; var item = window.vueApp.lists[index];
if(item.attachment_id === file.id){ if(item.attachment_id === file.id){
@@ -456,19 +453,19 @@
item.message = res.message; item.message = res.message;
item.state = "error"; item.state = "error";
} }
break;
} }
} }
}).on("beforeFileQueued",function (file) {
}).on("uploadComplete",function () {
}).on("uploadProgress",function (file, percentage) { }).on("uploadProgress",function (file, percentage) {
var $li = $( '#'+file.id ), var $li = $( '#'+file.id ),
$percent = $li.find('.progress .progress-bar'); $percent = $li.find('.progress .progress-bar');
$percent.css( 'width', percentage * 100 + '%' ); $percent.css( 'width', percentage * 100 + '%' );
}).on("error", function (type) {
if(type === "F_EXCEED_SIZE"){
layer.msg("文件超过了限定大小");
}
console.log(type);
}); });
}catch(e){ }catch(e){
console.log(e); console.log(e);