上传在中,是非常普遍的一项任务,以前用asp的时候,一直用稻农的无组件上传工具,觉得很好用,现在学asp.net了,却发现没有类似原来稻农的无组件上传程序,因此花了点时间,将稻农的无组件上传程序用vb.net改写了一下,可以编译成dll,在c#或者vb.net等任意asp.net支持的语言中使用,在共享出来,希望能为大家节约点时间,也欢迎提出意见和建议,让他更完善。
option explicit on
option strict on
imports system.io
imports system.data
imports system.web.ui.htmlcontrols.htmlinputcontrol
public class uploadfile
'-------------------------------------------------------------------
'欢迎转载,但请保留以下声名
'说明:文件上传类
'创建时间:2004-11-18
'作者:刀尖客 qq:51978456
--------------------------------------------------------------------
private locorgfilename as string '原始文件名
private locnewfilename as string '新文件名
private locuploaddir as string = "" '保存目录 注意:要完整路径
private locallowoverwrite as boolean = false '如果保存文件已经存在,是否覆盖
private locallowextfile as string = "doc,jpg,gif,zip" '许可格式
private locallowmaxsize as integer = 3 * 1024 * 1024 '许可大小
public errmsg as string '返回的错误提示
public readonly property orgfilename() as string
get
return locorgfilename
end get
end property
public property newfilename() as string
get
return locnewfilename
end get
set(byval strvalue as string)
locnewfilename = strvalue
end set
end property
public property uploaddir() as string
get
return locuploaddir
end get
set(byval strvalue as string)
locuploaddir = strvalue
end set
end property
public property allowoverwrite() as boolean
get
return locallowoverwrite
end get
set(byval blnvalue as boolean)
locallowoverwrite = blnvalue
end set
end property
public property allowmaxsize() as integer
get
return locallowmaxsize
end get
set(byval intvalue as integer)
locallowmaxsize = intvalue
end set
end property
public property allowextfile() as string
get
return locallowextfile
end get
set(byval strvalue as string)
locallowextfile = strvalue
end set
end property
'----------------------------------------------------------------------------------------
'功能说明:上传文件到服务器
'参数:file 要上传的文件域 israndom 是否采用随机数文件名,如果为flase,则采用文件原来的名称
'----------------------------------------------------------------------------------------
public function upload(byref file as system.web.ui.htmlcontrols.htmlinputfile, optional byval israndom as boolean = true) as boolean
dim objfile as file
dim oldfilename as string
dim strnewfilename as string
dim strextname as string
if file.postedfile.contentlength = 0 then
errmsg = "没有上传文件"
return false
end if
oldfilename = file.postedfile.filename
strextname = getextname(oldfilename)
if israndom = true then
locnewfilename = getrandomfilename(oldfilename, strextname)
strnewfilename = locnewfilename
else
locnewfilename = oldfilename
strnewfilename = locnewfilename
end if
if locuploaddir <> "" then
if directory.exists(locuploaddir) = false then
errmsg = "上传目录" & locuploaddir & "不存在"
else
if objfile.exists(strnewfilename) and locallowoverwrite = false then
errmsg = "对不起,服务器上此文件已经存在!"
return false
else
if instr(locallowextfile, strextname) <> 0 then
if file.postedfile.contentlength <= locallowmaxsize then
try
file.postedfile.saveas(locuploaddir & "/" & strnewfilename)
catch ex as exception
errmsg = ex.message
return false
end try
else
errmsg = "对不起,只允许上传小于" & locallowmaxsize & "字节的文件,上传文件超出最大的允许的大小!"
return false
end if
else
errmsg = "对不起,只允许上传以下格式的文件" & locallowextfile & "!"
return false
end if
end if
end if
else
errmsg = "上传目录未设置"
return false
end if
return true
end function
'-----------------------------------------------------------------------------------------
'功能说明:根据日期和时间得到一个不重复的随机数文件名
'-----------------------------------------------------------------------------------------
public function getrandomfilename(byval strfilename as string, byval strextname as string) as string
dim tempfilename as string
dim ext as string
tempfilename = cstr(now())
tempfilename = replace(tempfilename, "-", "")
tempfilename = replace(tempfilename, ":", "")
tempfilename = replace(tempfilename, " ", "")
tempfilename = tempfilename & getrandomnumber(1, 1000)
getrandomfilename = tempfilename & strextname
end function
'----------------------------------------------------------------------------------------
'功能说明:获取指定上下限内的随机数
'-----------------------------------------------------------------------------------------
public function getrandomnumber(optional byval low as integer = 1, optional byval high as integer = 100) as integer
dim orandom as system.random
orandom = new system.random(ctype(system.datetime.now.ticks mod system.int32.maxvalue, integer))
return orandom.next(low, high + 1)
end function
'------------------------------------------------------------------------------------
'功能说明:根据文件名,得到扩展名
'------------------------------------------------------------------------------------
public function getextname(byval strfilename as string) as string
return right(strfilename, instr(strreverse(strfilename), "."))
end function
end class
新闻热点
疑难解答
图片精选