I have a little file upload page that I have been able to use to successfully upload files to the C: drive of LocalHost (my machine). I need to be able to upload to a network drive from the intranet server. On the line: dirs = Directory.GetDi rectories(curre ntDir) I get "Access to the path "\\les-net\les\Special Projects\ATSPDF " is denied." How do I get the GetDirectories command to user my user ID and password when it tries to hit the network drive. The browser has a login where the UserID and Password are captured and stored in session variables that information is available. How do I cause GetDirectories to use that info?
If you need more information I have included the relevant HTML, VB.NET code and the Error message I get.
This is what I try:
In aspx page:
<FORM id=Form1 method=post enctype="multip art/form-data" runat="server">
<FONT face=verdana>Se lect File to Upload:</FONT> <INPUT
id=uploadedFile type=file name=uploadedFi le runat="server">
<INPUT id=upload type=button value=Upload name=upload runat="server">
</FORM>
In code behind:
Imports System.IO
Public Class UploadPDFFiles
Inherits System.Web.UI.P age
Dim currentDir As String
Dim directorySepara torChar As Char = Path.DirectoryS eparatorChar
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()
End Sub
Protected WithEvents dirContent As System.Web.UI.W ebControls.Labe l
Protected WithEvents message As System.Web.UI.W ebControls.Labe l
Protected WithEvents uploadedFile As System.Web.UI.H tmlControls.Htm lInputFile
Protected WithEvents upload As System.Web.UI.H tmlControls.Htm lInputButton
Protected WithEvents NavMenu1 As NavMenu.CustomC ontrols.NavMenu
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceho lderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeCompo nent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
sMenuItems = "Home;Home.aspx ;Home Page|"
sMenuItems &= "Search;Search. aspx;Find an Archive Box"
sMenuItems &= "Help;Help.aspx ;Help for this screen"
Navmenu1.NavMen uHeading = "Archive Tracking System"
Call InitNavMenu(Nav Menu1)
' Dim root As String = "C:\Inetpub\www root\ATS\PDF"
Dim root As String = "\\les-net\les\Special Projects\ATSPDF "
Dim thisPage As String = Request.Path
currentDir = Request.Params( "dir")
If currentDir Is Nothing Then
currentDir = root
End If
If Not currentDir.Star tsWith(root) Then
currentDir = root
End If
Dim sb As New System.Text.Str ingBuilder(4096 )
Try
If Not currentDir.Equa ls(Root) Then
' not at the root
Dim currentDirParen t As String
Dim lastIndex As Integer = _
currentDir.Last IndexOf(directo rySeparatorChar )
If lastIndex <> -1 Then
currentDirParen t = currentDir.Subs tring(0, lastIndex)
Else
currentDirParen t = currentDir
End If
sb.Append("<a href=").Append( thisPage)
sb.Append("?dir =").Append(Serv er.UrlEncode(cu rrentDirParent) )
sb.Append("><im g width=30 border=0 src=images/Up.gif></a><br>")
End If
sb.Append("<br> <img border=0 src=images/OpenFolder.gif> ")
sb.Append("<fon t face=verdana>")
sb.Append(curre ntDir)
sb.Append("</font>")
sb.Append("<br> ")
sb.Append("<tab le>")
sb.Append("<tr bgcolor=#D8D8D8 >")
sb.Append("<td width=200><font face=verdana size=3>Name</font></td>")
sb.Append("<td> <font face=verdana size=3>Type</font></td>")
sb.Append("<td> <font face=verdana size=3>Size</font></td>")
sb.Append("<td> <font face=verdana size=3>Modified </font></td>")
sb.Append("</tr>")
Catch ex As Exception
sException = ex
sErrorPage = "file upload"
Response.Redire ct("ErrorPage.a spx")
End Try
Try
Dim dirs() As String
dirs = Directory.GetDi rectories(curre ntDir)
Dim d As String
For Each d In dirs
Dim dirName As String = Path.GetFileNam e(d)
sb.Append("<tr> ")
sb.Append("<td> <img src=images/Folder.gif> ")
sb.Append("<a href=").Append( thisPage)
sb.Append("?dir =").Append(Serv er.UrlEncode(cu rrentDir))
sb.Append(direc torySeparatorCh ar)
sb.Append(Serve r.UrlEncode(dir Name))
sb.Append(">"). Append(dirName) .Append("</a>")
sb.Append("</td>")
sb.Append("<td> <font face=verdana size=2>folder</font></td>")
sb.Append("<td> </td>")
sb.Append("<td> <font face=verdana size=2>")
sb.Append(Direc tory.GetLastWri teTime(currentD ir & _
directorySepara torChar.ToStrin g() & dirName).ToStri ng())
sb.Append("</font></td>")
sb.Append("</tr>")
Next
Catch ex As Exception
sException = ex
sErrorPage = "file upload"
Response.Redire ct("ErrorPage.a spx")
End Try
Try
Dim dirInfo As New DirectoryInfo(c urrentDir)
Dim files() As FileInfo
files = dirInfo.GetFile s()
Dim f As FileInfo
For Each f In files
Dim filename As String = f.Name
sb.Append("<tr> ")
sb.Append("<td> <img src=images/File.gif> ")
sb.Append("<a href=FileDownlo ad.aspx?file=")
sb.Append(Serve r.UrlEncode(cur rentDir))
sb.Append(direc torySeparatorCh ar)
sb.Append(Serve r.UrlEncode(fil ename))
sb.Append(">"). Append(filename ).Append("</a>")
sb.Append("</td>")
sb.Append("<td> <font face=verdana size=2>file</font></td>")
sb.Append("<td> <font face=verdana size=2>")
sb.Append(f.Len gth.ToString())
sb.Append("</font></td>")
sb.Append("<td> <font face=verdana size=2>")
sb.Append(File. GetLastWriteTim e(currentDir & _
directorySepara torChar.ToStrin g() & f.Name).ToStrin g())
sb.Append("</font></td>")
sb.Append("</tr>")
Next
Catch ex As Exception
sException = ex
sErrorPage = "file upload"
Response.Redire ct("ErrorPage.a spx")
End Try
sb.Append("</table>")
dirContent.Text = sb.ToString()
End Sub
Private Sub upload_ServerCl ick(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles upload.ServerCl ick
If Not (uploadedFile.P ostedFile Is Nothing) Then
Try
Dim postedFile = uploadedFile.Po stedFile
Dim filename As String = Path.GetFileNam e(postedFile.Fi leName)
Dim contentType As String = postedFile.Cont entType
Dim contentLength As Integer = postedFile.Cont entLength
postedFile.Save As(currentDir & directorySepara torChar.ToStrin g() & filename)
Catch ex As Exception
sException = ex
sErrorPage = "file upload"
Response.Redire ct("ErrorPage.a spx")
End Try
End If
End Sub
End Class
The error I get is this:
A system error has occurred at:
file upload
System.Unauthor izedAccessExcep tion: Access to the path "\\les-net\les\Special Projects\ATSPDF " is denied. at System.IO.__Err or.WinIOError(I nt32 errorCode, String str) at System.IO.Direc tory.InternalGe tFileDirectoryN ames(String fullPath, String userPath, Boolean file) at System.IO.Direc tory.InternalGe tDirectories(St ring path, String userPath, String searchPattern) at System.IO.Direc tory.GetDirecto ries(String path, String searchPattern) at System.IO.Direc tory.GetDirecto ries(String path) at ATS.UploadPDFFi les.Page_Load(O bject sender, EventArgs e) in c:\inetpub\wwwr oot\ATS\UploadP DFFiles.aspx.vb :line 87
Access to the path "\\les-net\les\Special Projects\ATSPDF " is denied.
Thanks for any help!
If you need more information I have included the relevant HTML, VB.NET code and the Error message I get.
This is what I try:
In aspx page:
<FORM id=Form1 method=post enctype="multip art/form-data" runat="server">
<FONT face=verdana>Se lect File to Upload:</FONT> <INPUT
id=uploadedFile type=file name=uploadedFi le runat="server">
<INPUT id=upload type=button value=Upload name=upload runat="server">
</FORM>
In code behind:
Imports System.IO
Public Class UploadPDFFiles
Inherits System.Web.UI.P age
Dim currentDir As String
Dim directorySepara torChar As Char = Path.DirectoryS eparatorChar
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()
End Sub
Protected WithEvents dirContent As System.Web.UI.W ebControls.Labe l
Protected WithEvents message As System.Web.UI.W ebControls.Labe l
Protected WithEvents uploadedFile As System.Web.UI.H tmlControls.Htm lInputFile
Protected WithEvents upload As System.Web.UI.H tmlControls.Htm lInputButton
Protected WithEvents NavMenu1 As NavMenu.CustomC ontrols.NavMenu
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceho lderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeCompo nent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
sMenuItems = "Home;Home.aspx ;Home Page|"
sMenuItems &= "Search;Search. aspx;Find an Archive Box"
sMenuItems &= "Help;Help.aspx ;Help for this screen"
Navmenu1.NavMen uHeading = "Archive Tracking System"
Call InitNavMenu(Nav Menu1)
' Dim root As String = "C:\Inetpub\www root\ATS\PDF"
Dim root As String = "\\les-net\les\Special Projects\ATSPDF "
Dim thisPage As String = Request.Path
currentDir = Request.Params( "dir")
If currentDir Is Nothing Then
currentDir = root
End If
If Not currentDir.Star tsWith(root) Then
currentDir = root
End If
Dim sb As New System.Text.Str ingBuilder(4096 )
Try
If Not currentDir.Equa ls(Root) Then
' not at the root
Dim currentDirParen t As String
Dim lastIndex As Integer = _
currentDir.Last IndexOf(directo rySeparatorChar )
If lastIndex <> -1 Then
currentDirParen t = currentDir.Subs tring(0, lastIndex)
Else
currentDirParen t = currentDir
End If
sb.Append("<a href=").Append( thisPage)
sb.Append("?dir =").Append(Serv er.UrlEncode(cu rrentDirParent) )
sb.Append("><im g width=30 border=0 src=images/Up.gif></a><br>")
End If
sb.Append("<br> <img border=0 src=images/OpenFolder.gif> ")
sb.Append("<fon t face=verdana>")
sb.Append(curre ntDir)
sb.Append("</font>")
sb.Append("<br> ")
sb.Append("<tab le>")
sb.Append("<tr bgcolor=#D8D8D8 >")
sb.Append("<td width=200><font face=verdana size=3>Name</font></td>")
sb.Append("<td> <font face=verdana size=3>Type</font></td>")
sb.Append("<td> <font face=verdana size=3>Size</font></td>")
sb.Append("<td> <font face=verdana size=3>Modified </font></td>")
sb.Append("</tr>")
Catch ex As Exception
sException = ex
sErrorPage = "file upload"
Response.Redire ct("ErrorPage.a spx")
End Try
Try
Dim dirs() As String
dirs = Directory.GetDi rectories(curre ntDir)
Dim d As String
For Each d In dirs
Dim dirName As String = Path.GetFileNam e(d)
sb.Append("<tr> ")
sb.Append("<td> <img src=images/Folder.gif> ")
sb.Append("<a href=").Append( thisPage)
sb.Append("?dir =").Append(Serv er.UrlEncode(cu rrentDir))
sb.Append(direc torySeparatorCh ar)
sb.Append(Serve r.UrlEncode(dir Name))
sb.Append(">"). Append(dirName) .Append("</a>")
sb.Append("</td>")
sb.Append("<td> <font face=verdana size=2>folder</font></td>")
sb.Append("<td> </td>")
sb.Append("<td> <font face=verdana size=2>")
sb.Append(Direc tory.GetLastWri teTime(currentD ir & _
directorySepara torChar.ToStrin g() & dirName).ToStri ng())
sb.Append("</font></td>")
sb.Append("</tr>")
Next
Catch ex As Exception
sException = ex
sErrorPage = "file upload"
Response.Redire ct("ErrorPage.a spx")
End Try
Try
Dim dirInfo As New DirectoryInfo(c urrentDir)
Dim files() As FileInfo
files = dirInfo.GetFile s()
Dim f As FileInfo
For Each f In files
Dim filename As String = f.Name
sb.Append("<tr> ")
sb.Append("<td> <img src=images/File.gif> ")
sb.Append("<a href=FileDownlo ad.aspx?file=")
sb.Append(Serve r.UrlEncode(cur rentDir))
sb.Append(direc torySeparatorCh ar)
sb.Append(Serve r.UrlEncode(fil ename))
sb.Append(">"). Append(filename ).Append("</a>")
sb.Append("</td>")
sb.Append("<td> <font face=verdana size=2>file</font></td>")
sb.Append("<td> <font face=verdana size=2>")
sb.Append(f.Len gth.ToString())
sb.Append("</font></td>")
sb.Append("<td> <font face=verdana size=2>")
sb.Append(File. GetLastWriteTim e(currentDir & _
directorySepara torChar.ToStrin g() & f.Name).ToStrin g())
sb.Append("</font></td>")
sb.Append("</tr>")
Next
Catch ex As Exception
sException = ex
sErrorPage = "file upload"
Response.Redire ct("ErrorPage.a spx")
End Try
sb.Append("</table>")
dirContent.Text = sb.ToString()
End Sub
Private Sub upload_ServerCl ick(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles upload.ServerCl ick
If Not (uploadedFile.P ostedFile Is Nothing) Then
Try
Dim postedFile = uploadedFile.Po stedFile
Dim filename As String = Path.GetFileNam e(postedFile.Fi leName)
Dim contentType As String = postedFile.Cont entType
Dim contentLength As Integer = postedFile.Cont entLength
postedFile.Save As(currentDir & directorySepara torChar.ToStrin g() & filename)
Catch ex As Exception
sException = ex
sErrorPage = "file upload"
Response.Redire ct("ErrorPage.a spx")
End Try
End If
End Sub
End Class
The error I get is this:
A system error has occurred at:
file upload
System.Unauthor izedAccessExcep tion: Access to the path "\\les-net\les\Special Projects\ATSPDF " is denied. at System.IO.__Err or.WinIOError(I nt32 errorCode, String str) at System.IO.Direc tory.InternalGe tFileDirectoryN ames(String fullPath, String userPath, Boolean file) at System.IO.Direc tory.InternalGe tDirectories(St ring path, String userPath, String searchPattern) at System.IO.Direc tory.GetDirecto ries(String path, String searchPattern) at System.IO.Direc tory.GetDirecto ries(String path) at ATS.UploadPDFFi les.Page_Load(O bject sender, EventArgs e) in c:\inetpub\wwwr oot\ATS\UploadP DFFiles.aspx.vb :line 87
Access to the path "\\les-net\les\Special Projects\ATSPDF " is denied.
Thanks for any help!
Comment