1

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

   <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 LinkBut​​ton1_Click(sender As Object, e As EventArgs) 处理 LinkBut​​ton1.Click

If FileUpload1.HasFile Then 尝试 暗淡文件 = FileUpload1.PostedFile.ContentType 暗淡扩展为字符串 = Path.GetExtension(FileUpload1.FileName) 如果扩展 = ".jpeg" 或者其他扩展 = ".jpg" 那么

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
4

1 回答 1

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"
于 2013-07-14T19:21:37.647 回答