如果有源代码更好啊!
如果有源代码更好啊!
這是我曾經做過的一種方法,你做過去參考一下吧:
Private Function UploadFile(ByVal InputFile As HtmlInputFile, ByVal UpLoadPath As String) As Boolean '上傳文件
Dim boolReturnValue As Boolean = False
Try
Dim postedFile As System.Web.HttpPostedFile
Dim strFileName As String
Dim strFileExtendName As String
Dim strServerPath As String
postedFile = InputFile.PostedFile
If postedFile.ContentLength > 0 Then '判斷是否點選了要上傳的文件
strFileName = System.IO.Path.GetFileNameWithoutExtension(postedFile.FileName) '得到沒有擴展名的文件名
strFileExtendName = System.IO.Path.GetExtension(postedFile.FileName) '得到該文件的擴展名
If System.IO.Directory.Exists(UpLoadPath) = False Then '如果該文件目錄不存在,測創建
System.IO.Directory.CreateDirectory(UpLoadPath)
End If
'映射到服務器端的文件路徑
strServerPath = UpLoadPath & strFileName & Now.Date.ToString("yyyyMMdd") & Now.Hour & Now.Minute & Now.Second & strFileExtendName '文件名由年月日時分秒組成
Session("Path") = strServerPath
postedFile.SaveAs(strServerPath) '上傳
boolReturnValue = True
End If
Catch ex As Exception
Finally
End Try
Return boolReturnValue
End Function