Protected Sub btnadd_sylbus_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnadd_sylbus.Click
Dim extension As String = System.IO.Path.GetExtension(FileUpload_sylbus.PostedFile.FileName).ToLower()
Dim Type As String = Nothing
If (extension = ".pdf") Then
Dim intFileNameLength As Integer
Dim strFileNamePath As String
Dim strFileNameOnly As String
strFileNamePath = FileUpload_sylbus.PostedFile.FileName
intFileNameLength = InStr(1, StrReverse(strFileNamePath), "\")
strFileNameOnly = Mid(strFileNamePath, (Len(strFileNamePath) - intFileNameLength) + 2)
Dim br As New BinaryReader(FileUpload_sylbus.PostedFile.InputStream)
FileUpload_sylbus.PostedFile.SaveAs("D:\OLMS\" & strFileNameOnly)
cmd.CommandText = "INSERT into Syllabus(sylbus_id, sylbus_name, file_name, content) values(@id,@name,@file,@cont)"
cmd.Connection = cn
cmd.Parameters.Add("@id", txtsylbus_id.Text)
cmd.Parameters.Add("@name", txtsylbus_name.Text)
cmd.Parameters.Add("@file", FileUpload_sylbus.FileName)
cmd.Parameters.Add("@cont", br.ReadBytes(FileUpload_sylbus.PostedFile.ContentLength))
cmd.ExecuteNonQuery()
cn.Close()
lbladd_sylbus.Visible = True
lbladd_sylbus.Text = "File Upload Success."
txtsylbus_id.Text = Nothing
txtsylbus_name.Text = Nothing
Else
lbladd_sylbus.Visible = True
lbladd_sylbus.Text = "Not a Valid file format"
End If
End Sub
答案 0 :(得分:0)
你可以试试这段代码
strFileNamePath = FileUpload_sylbus.PostedFile.FileName
strFileNameOnly = Path.GetFilename(strFileNamePath)
Dim newFileNamePath as String = Path.Combine("D:\OLMS", strFileNameOnly)
Dim br As New BinaryReader(FileUpload_sylbus.PostedFile.InputStream)
FileUpload_sylbus.PostedFile.SaveAs(newFileNamePath)
现在,如果在访问文件夹D:\OLMS
时仍然出现错误,那么您在站点的根文件夹之外有此路径,并且您的计算机配置(搜索MEDIUM TRUST)不允许在根文件夹之外写入。 (当然我假设文件夹D:\OLMS
存在,运行IIS ASP.NET服务的用户具有在该文件夹中写入的所有权限)