'保存文件函数
'上传文件名,文件内容
function saveFile(byval filename, byval savepath, byval filedata)
on error resume next
dim fext, fname, fpath, oStream
fname = repByRegx("\s|:|-|\/|\\|\.", cstr(now()) & cstr(timer()), "") & "_" & ranStr(1,9,2)
fext = trim(Lcase(Mid(filename,InStrRev(filename, ".")+1)))
if lenb(filedata) / 1024 > 1024 then '1024 KB
saveFile = "error:3"
exit function
end if
fpath = savepath
'如果文件夹不存在,创建
if not file_exists(Server.MapPath(fpath)) then
createFolder(Server.MapPath(fpath))
end if
set oStream = Server.CreateObject("adodb.stream")
oStream.Type = 1
oStream.Mode = 3
oStream.Open
oStream.Write filedata
oStream.Position = 0
oStream.SavetoFile Server.MapPath(fpath & fname & "." & fext), 2
oStream.Close
set oStream = nothing
if err then '保存文件失败
saveFile = ""
err.clear
exit function
end if
saveFile = fpath & fname & "." & fext'不要带虚拟目录路径
end function
function createFolder(byval crefolder)
on error resume next
Set oFso=Server.CreateObject("Scripting.FileSystemObject")
oFso.CreateFolder(crefolder)
Set oFso=nothing
err.clear
end function
'字符串,正则,替换值
function repByRegx(patrn, str ,repv)
dim regEx
set regEx = new RegExp
regEx.Global = true
regEx.IgnoreCase = false
regEx.Pattern = patrn
repByRegx = regEx.replace(str, repv)
set regEx = nothing
end function
function file_exists(byval filename)
dim func_fso
set func_fso = Server.CreateObject("Scripting.FileSystemObject")
file_exists = func_fso.fileexists(null_val(filename,""))
if not file_exists then
file_exists = func_fso.folderexists(null_val(filename,""))
end if
set func_fso = nothing
end function
'随机数,开始,结束,长度从1开始计算
function ranStr(byval s,byval e, byval l)
Dim r
Randomize '对随机数生成器做初始化的动作。
for ii = 1 to l
r = Int((e * Rnd) + s) ' 生成 s 到 e 之间的随机数值
ranStr = ranStr & r
next
end function
function null_val(byval str, byval val)
if isnull(str) then
null_val = val
exit function
elseif trim(cstr(str)) = "" then
null_val = val
exit function
end if
null_val = cstr(str)
end function
'////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
'使用方法一'html文件
<form method="post" action="upload.asp" enctype="multipart/form-data">
<input name="text1", id="text1" type="text" />
<input name="file1" id="file1" type="file" />
<input name="file2" id="file2" type="file" />
<input type="submit />
</form>
'处理ASP文件
dim text1, upfile1, upfile2
dim formDataList, formFileList
dim formDivider(1)
Set formDataList = CreateObject("Scripting.Dictionary") '数据保存
Set formFileList = CreateObject("Scripting.Dictionary") '数据保存
if len(getFormData(formDataList, formFileList, formDivider)) = 0 then
text1 = formDataList("text1")
upfile1= saveFile(formFileList("file1"), "upload/", formDataList("file1"))
upfile2= saveFile(formFileList("file2"), "upload/", formDataList("file2"))
end if
'使用方法二'html文件
<form method="post" action="upload.asp" enctype="multipart/form-data">
<input name="text1", id="text1" type="text" />
<input name="text1", id="text1" type="text" />
<input name="file1" id="file1" type="file" />
<input name="file1" id="file1" type="file" />
<input type="submit />
</form>
'处理ASP文件
dim text1, text2, upfile1, upfile2
dim formDataList, formFileList
dim formDivider(1)
Set formDataList = CreateObject("Scripting.Dictionary") '数据保存
Set formFileList = CreateObject("Scripting.Dictionary") '数据保存
if len(getFormData(formDataList, formFileList, formDivider)) = 0 then
'二进制是用 formDivider(0) 分割,如上传文件内容, 其它用 formDivider(1)分割
text1 = split(formDataList("text1"), formDivider(1))(0)
text2 = split(formDataList("text1"), formDivider(1))(1)
upfile1= saveFile(split(formFileList("file1"), formDivider(1))(0), "upload/", split(formDataList("file1"), formDivider(0))(0))
upfile1= saveFile(split(formFileList("file1"), formDivider(1))(1), "upload/", split(formDataList("file1"), formDivider(0))(1))
end if