上傳在中,是非常普遍的一項任務,以前用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
新聞熱點
疑難解答
圖片精選