现介段在学习asp.net2.0今天写了个文件上传的函数.很不完善.欢迎各位网友给点意见:.
'函数名:UpFile'功能:实现文件上传'输入参数:inputfile 上传文件的本地路径 ' filePaht 存入服务器中的文件夹名称 ' MyfileName 指定一个文件名 ' isRandom 产生新的文件名 '返回值: 为一个 FileUpload 类型
Private Function UpFile(ByVal inputfile As FileUpload, ByVal FilePaht As String, ByVal MyfileName As String, ByVal isRandom As Boolean)
Dim fp As FileUpload = New FileUpload
Dim fileok As Boolean = False
Dim FileName As String = "", svaeName As String = "", fileExtension As String = "" Dim postfile As HttpPostedFile = inputfile.PostedFile
FileName = System.IO.Path.GetFileName(postfile.FileName)
fileExtension = System.IO.Path.GetExtension(FileName)
Dim allowedExtensions As String() = {".jpg", ".jpeg", ".png", ".gif"}
If Array.IndexOf(allowedExtensions, fileExtension) = -1 Then Return fp
If FileName <> "" Then FileName = MyfileName
If isRandom Then
Dim objRan As Random = New Random
Dim DateName As DateTime = DateTime.Now
svaeName = Date.Now.Year.ToString & Date.Now.Month.ToString & Date.Now.Day.ToString & _
Date.Now.Hour.ToString & Date.Now.Minute.ToString & Date.Now.Second.ToString & _
Convert.ToString(objRan.Next(99) * 97 + 100)
FileName = svaeName + fileExtension
End If
Dim phypath As String = HttpContext.Current.Request.MapPath(FilePaht)
Dim upDir As DirectoryInfo = New DirectoryInfo(phypath) '//判断路径是否存在,若不存在则创建路径
If (Not upDir.Exists) Then upDir.Create()
Try
postfile.SaveAs(phypath & "\" & FileName) '保存
Catch ex As Exception
Response.Output.Write(ex.Message)
End Try
Return fp
End Function
'函数名:UpFile'功能:实现文件上传'输入参数:inputfile 上传文件的本地路径 ' filePaht 存入服务器中的文件夹名称 ' MyfileName 指定一个文件名 ' isRandom 产生新的文件名 '返回值: 为一个 FileUpload 类型
Private Function UpFile(ByVal inputfile As FileUpload, ByVal FilePaht As String, ByVal MyfileName As String, ByVal isRandom As Boolean)
Dim fp As FileUpload = New FileUpload
Dim fileok As Boolean = False
Dim FileName As String = "", svaeName As String = "", fileExtension As String = "" Dim postfile As HttpPostedFile = inputfile.PostedFile
FileName = System.IO.Path.GetFileName(postfile.FileName)
fileExtension = System.IO.Path.GetExtension(FileName)
Dim allowedExtensions As String() = {".jpg", ".jpeg", ".png", ".gif"}
If Array.IndexOf(allowedExtensions, fileExtension) = -1 Then Return fp
If FileName <> "" Then FileName = MyfileName
If isRandom Then
Dim objRan As Random = New Random
Dim DateName As DateTime = DateTime.Now
svaeName = Date.Now.Year.ToString & Date.Now.Month.ToString & Date.Now.Day.ToString & _
Date.Now.Hour.ToString & Date.Now.Minute.ToString & Date.Now.Second.ToString & _
Convert.ToString(objRan.Next(99) * 97 + 100)
FileName = svaeName + fileExtension
End If
Dim phypath As String = HttpContext.Current.Request.MapPath(FilePaht)
Dim upDir As DirectoryInfo = New DirectoryInfo(phypath) '//判断路径是否存在,若不存在则创建路径
If (Not upDir.Exists) Then upDir.Create()
Try
postfile.SaveAs(phypath & "\" & FileName) '保存
Catch ex As Exception
Response.Output.Write(ex.Message)
End Try
Return fp
End Function
现在我有一个问题没有很好的解决.我试了一下是不么,就是我想在判断出它的格式不对.就返回并且给予提示.
我不知道写得怎么样.希望各位网友提出来.或者各位网友有更好的方法就请拿出来共享一下