我正在尝试将文件上传到新创建的目录, 我设法创建它但不能将文件放在那里因为访问被拒绝, 如何允许访问目录?
<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.ClickIf 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
答案 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"