Access to path [Destination Address] is denied - when creating a new directory - VB.NET

StackOverflow https://stackoverflow.com/questions/17642227

  •  03-06-2022
  •  | 
  •  

Вопрос

I am trying to upload a file to a newly created directory, i manage to create it but can not place the file there because access is denied, How can i allow the acces to the directory?

   <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
Это было полезно?

Решение

I am not sure of you have the error in the CreateDirectory call or afterward. I am sure that the FileUpload control doesn't have a SaveAs method and this is on the PostedFile object and that you need to pass a FileName not a Directory name

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"
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top