在创建新目录时,拒绝访问路径[目标地址] - VB.NET

时间:2013-07-14 18:15:00

标签: asp.net vb.net access-denied

我正在尝试将文件上传到新创建的目录,  我设法创建它但不能将文件放在那里因为访问被拒绝,  如何允许访问目录?

   <asp:FileUpload ID="FileUpload1" runat="server" />
   <asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
   <asp:Label ID="Label1" runat="server" Text="Upload Status"></asp:Label>

VB.NET

Protected Sub LinkButton1_Click(sender As Object, e As EventArgs) Handles LinkButton1.Click

If FileUpload1.HasFile Then Try Dim file = FileUpload1.PostedFile.ContentType Dim Extention As String = Path.GetExtension(FileUpload1.FileName) If Extention = ".jpeg" OrElse Extention = ".jpg" Then

If FileUpload1.PostedFile.ContentLength > 2097152 Then Label1.Text = "File Too Big" Else Dim directoryPath As String = Server.MapPath("~/bussnisses") If Not Directory.Exists(directoryPath) Then Directory.CreateDirectory(directoryPath) FileUpload1.SaveAs(directoryPath) Else FileUpload1.SaveAs(directoryPath) End If Label1.Text = "complete" End If Else Label1.Text = " jpeg or jpg" End If Catch ex As Exception End Try End If End Sub

1 个答案:

答案 0 :(得分:0)

我不确定您在CreateDirectory调用中或之后是否有错误。我确信FileUpload控件没有SaveAs方法,这是在PostedFile对象上,你需要传递FileName而不是目录名

Dim directoryPath As String = Server.MapPath("~/bussnisses")
If Not Directory.Exists(directoryPath) Then
    Directory.CreateDirectory(directoryPath)
End If

Dim destFile = Path.GetFileName(FileUpload1.PostedFile.FileName)
destFile = Path.Combine(directoryPath, destFile)
FileUpload1.PostedFile.SaveAs(destFile)
Label1.Text = "complete"