kindeditor上传插件分类保存路径细分修改

  kindeditor的上传的文件(包括图片)默认是保存在attached/(image|flash|media|file)/当前日期年月日 对应的目录下,依据加载的插件,自动给uploadJson配置的url增加dir参数。

 

  如果你需要修改attached保存路径或者(image|flash|media|file)这种分类目录,需要手动修改upload_json.asp文件,给upload_json.asp增加功能。

 

  下面为一个示例,将attached下专门保存图片的image目录修改为images,并且对上传的图片再次分类保存,在images下增加目录。

 

  首先,配置kindeditor的uploadJson,给url增加一个特殊的参数,传递分类目录名称,源代码如下

<!doctype html>
<html>
	<head>
		<meta charset="gb2312" />
		<title>kindeditor上传插件分类保存路径细分修改</title>
		<style>
			form {
				margin: 0;
			}
			textarea {
				display: block;
			}
		</style>
		<script charset="utf-8" src="../kindeditor-min.js"></script>
		<script charset="utf-8" src="../lang/zh_CN.js"></script>
		<script>
		    KindEditor.ready(function (K) {
		        var editor = K.editor({
		            allowFileManager: true,
		            //配置图片分类目录下的不同目录参数,如果继续进行分类,可以传递?fd=xxoo/abc而这种参数,注意不要以/结束,程序未判断,以/结束程序会
		            uploadJson: '../asp/upload_json.asp?fd=xxoo'
		        });
		        K('#image').click(function () {
		            editor.loadPlugin('image', function () {
		                editor.plugin.imageDialog({
		                    imageUrl: K('#url').val(),
		                    clickFn: function (url, title, width, height, border, align) {
		                        K('#url').val(url);
		                        editor.hideDialog();
		                    }
		                });
		            });
		        });
		    });
</script>
	</head>
	<body><input type="text" id="url" value="" style="width:600px" /> <input type="button" id="image" value="选择图片" />
	</body>
</html>

  其次,image目录修改为images


  有2个步骤,其实就一个步骤也行了,为了完整起见,最好连image插件的js添加dir参数一起修改了(这步骤省略也行)。


1)修改plugins\image\image.js

//'<form class="ke-upload-area ke-form" method="post" enctype="multipart/form-data" target="' + target + '" action="' + K.addParam(uploadJson, 'dir=image') + '">'
//===>
'<form class="ke-upload-area ke-form" method="post" enctype="multipart/form-data" target="' + target + '" action="' + K.addParam(uploadJson, 'dir=images') + '">'

2)修改upload_json.asp,下面为修改好的源代码,看注释

<%@ CODEPAGE=65001 %>
<% Option Explicit %>
<% Response.CodePage=65001 %>
<% Response.Charset="UTF-8" %>
<!--#include file="UpLoad_Class.asp"-->
<!--#include file="JSON_2.0.4.asp"-->
<%
' KindEditor ASP
'
' 本ASP程序是演示程序,建议不要直接在实际项目中使用。
' 如果您确定直接使用本程序,使用之前请仔细确认相关安全设置。
'
Dim aspUrl, savePath, saveUrl, maxSize, fileName, fileExt, newFileName, filePath, fileUrl, dirName
Dim extStr, imageExtStr, flashExtStr, mediaExtStr, fileExtStr
Dim upload, file, fso, ranNum, hash, ymd, mm, dd, result
aspUrl = Request.ServerVariables("SCRIPT_NAME")
aspUrl = left(aspUrl, InStrRev(aspUrl, "/"))
'文件保存目录路径
savePath = "../attached/"'''''''''如果要改变根目录,修改这里
'文件保存目录URL
saveUrl = aspUrl & "../attached/"
'定义允许上传的文件扩展名
imageExtStr = "gif|jpg|jpeg|png|bmp"
flashExtStr = "swf|flv"
mediaExtStr = "swf|flv|mp3|wav|wma|wmv|mid|avi|mpg|asf|rm|rmvb"
fileExtStr = "doc|docx|xls|xlsx|ppt|htm|html|txt|zip|rar|gz|bz2"
'最大文件大小
maxSize = 5 * 1024 * 1024 '5M
Set fso = Server.CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(Server.mappath(savePath)) Then
	showError("上传目录不存在。")
End If
dirName = Request.QueryString("dir")
if dirName="image" then dirName="images"'这里增加了分类目录的判断,所以是否修改plugins\image\image.js无关紧要
If isEmpty(dirName) Then'''''''''''附件下的分类目录,如果未传递默认为images
	dirName = "images"
End If
If instr(lcase("images,flash,media,file"), dirName) < 1 Then'附件下的分类目录名称
	showError("目录名不正确。")
End If
Select Case dirName
	Case "flash" extStr = flashExtStr
	Case "media" extStr = mediaExtStr
	Case "file" extStr = fileExtStr
	Case Else  extStr = imageExtStr
End Select
set upload = new AnUpLoad
upload.Exe = extStr
upload.MaxSize = maxSize
upload.GetData()
if upload.ErrorID>0 then 
	showError(upload.Description)
end if
savePath = savePath & dirName & "/"
saveUrl = saveUrl & dirName & "/"
'创建分类目录下的子目录,如果传递了fd参数的话
dim fd
fd=request.QueryString("fd")
if not isempty(fd) then
  savePath = savePath & fd& "/"
  saveUrl = saveUrl  & fd& "/"
end if
Function CreateFolder(ByVal folderPath )'循环创建目录函数
		Dim oFSO
		Set oFSO = createObject("Scripting.FileSystemObject")
		Dim sParent 
		sParent = oFSO.GetParentFolderName(folderPath)
		If sParent = "" Then Exit Function
		If Not oFSO.FolderExists(sParent) Then CreateFolder (sParent)
		If Not oFSO.FolderExists(folderPath) Then oFSO.CreateFolder (folderPath)
		Set oFSO = Nothing
End Function
'分类目录下再按照当前时间生成文件夹
mm = month(now)
If mm < 10 Then
	mm = "0" & mm
End If
dd = day(now)
If dd < 10 Then
	dd = "0" & dd
End If
ymd = year(now) & mm & dd
savePath = savePath & ymd & "/"
saveUrl = saveUrl & ymd & "/"
'创建目录结构
If Not fso.FolderExists(Server.mappath(savePath)) Then CreateFolder(Server.mappath(savePath))
set file = upload.files("imgFile")
if file is nothing then
	showError("请选择文件。")
end if
response.Write savePath
set result = file.saveToFile(savePath, 0, true)
if result.error then
	showError(file.Exception)
end if
filePath = Server.mappath(savePath & file.filename)
fileUrl = saveUrl & file.filename
Set upload = nothing
Set file = nothing
If Not fso.FileExists(filePath) Then
	showError("上传文件失败。")
End If
Response.AddHeader "Content-Type", "text/html; charset=UTF-8"
Set hash = jsObject()
hash("error") = 0
hash("url") = fileUrl
hash.Flush
Response.End
Function showError(message)
	Response.AddHeader "Content-Type", "text/html; charset=UTF-8"
	Dim hash
	Set hash = jsObject()
	hash("error") = 1
	hash("message") = message
	hash.Flush
	Response.End
End Function
%>

kindeditor上传插件分类保存路径细分修改

加支付宝好友偷能量挖...


原创文章,转载请注明出处:kindeditor上传插件分类保存路径细分修改

评论(0)Web开发网
阅读(1035)喜欢(0)JavaScript/Ajax开发技巧