Upload files to UNC network path from webserver

T

Tom Wells

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.GetDirectories(currentDir) 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="multipart/form-data" runat="server">
<FONT face=verdana>Select File to Upload:</FONT> <INPUT
id=uploadedFile type=file name=uploadedFile 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.Page
Dim currentDir As String
Dim directorySeparatorChar As Char = Path.DirectorySeparatorChar
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents dirContent As System.Web.UI.WebControls.Label
Protected WithEvents message As System.Web.UI.WebControls.Label
Protected WithEvents uploadedFile As System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents upload As System.Web.UI.HtmlControls.HtmlInputButton
Protected WithEvents NavMenu1 As NavMenu.CustomControls.NavMenu
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) 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.NavMenuHeading = "Archive Tracking System"
Call InitNavMenu(NavMenu1)
' Dim root As String = "C:\Inetpub\wwwroot\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.StartsWith(root) Then
currentDir = root
End If
Dim sb As New System.Text.StringBuilder(4096)
Try
If Not currentDir.Equals(Root) Then
' not at the root
Dim currentDirParent As String
Dim lastIndex As Integer = _
currentDir.LastIndexOf(directorySeparatorChar)
If lastIndex <> -1 Then
currentDirParent = currentDir.Substring(0, lastIndex)
Else
currentDirParent = currentDir
End If
sb.Append("<a href=").Append(thisPage)
sb.Append("?dir=").Append(Server.UrlEncode(currentDirParent))
sb.Append("><img width=30 border=0 src=images/Up.gif></a><br>")
End If
sb.Append("<br><img border=0 src=images/OpenFolder.gif> ")
sb.Append("<font face=verdana>")
sb.Append(currentDir)
sb.Append("</font>")
sb.Append("<br>")
sb.Append("<table>")
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.Redirect("ErrorPage.aspx")
End Try
Try
Dim dirs() As String
dirs = Directory.GetDirectories(currentDir)
Dim d As String
For Each d In dirs
Dim dirName As String = Path.GetFileName(d)
sb.Append("<tr>")
sb.Append("<td><img src=images/Folder.gif> ")
sb.Append("<a href=").Append(thisPage)
sb.Append("?dir=").Append(Server.UrlEncode(currentDir))
sb.Append(directorySeparatorChar)
sb.Append(Server.UrlEncode(dirName))
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(Directory.GetLastWriteTime(currentDir & _
directorySeparatorChar.ToString() & dirName).ToString())
sb.Append("</font></td>")
sb.Append("</tr>")
Next
Catch ex As Exception
sException = ex
sErrorPage = "file upload"
Response.Redirect("ErrorPage.aspx")
End Try
Try
Dim dirInfo As New DirectoryInfo(currentDir)
Dim files() As FileInfo
files = dirInfo.GetFiles()
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=FileDownload.aspx?file=")
sb.Append(Server.UrlEncode(currentDir))
sb.Append(directorySeparatorChar)
sb.Append(Server.UrlEncode(filename))
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.Length.ToString())
sb.Append("</font></td>")
sb.Append("<td><font face=verdana size=2>")
sb.Append(File.GetLastWriteTime(currentDir & _
directorySeparatorChar.ToString() & f.Name).ToString())
sb.Append("</font></td>")
sb.Append("</tr>")
Next
Catch ex As Exception
sException = ex
sErrorPage = "file upload"
Response.Redirect("ErrorPage.aspx")
End Try
sb.Append("</table>")
dirContent.Text = sb.ToString()
End Sub
Private Sub upload_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles upload.ServerClick
If Not (uploadedFile.PostedFile Is Nothing) Then
Try
Dim postedFile = uploadedFile.PostedFile
Dim filename As String = Path.GetFileName(postedFile.FileName)
Dim contentType As String = postedFile.ContentType
Dim contentLength As Integer = postedFile.ContentLength
postedFile.SaveAs(currentDir & directorySeparatorChar.ToString() & filename)
Catch ex As Exception
sException = ex
sErrorPage = "file upload"
Response.Redirect("ErrorPage.aspx")
End Try
End If
End Sub
End Class

The error I get is this:
A system error has occurred at:
file upload

System.UnauthorizedAccessException: Access to the path "\\les-net\les\Special Projects\ATSPDF" is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String str) at System.IO.Directory.InternalGetFileDirectoryNames(String fullPath, String userPath, Boolean file) at System.IO.Directory.InternalGetDirectories(String path, String userPath, String searchPattern) at System.IO.Directory.GetDirectories(String path, String searchPattern) at System.IO.Directory.GetDirectories(String path) at ATS.UploadPDFFiles.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\ATS\UploadPDFFiles.aspx.vb:line 87

Access to the path "\\les-net\les\Special Projects\ATSPDF" is denied.

Thanks for any help!
 
K

Kevin Spencer

The uthentication used in the browser allows the logged-in user to view your
ASP.Net pages. ASP.Net (your app) always runs under the same, single user
account. That account must be granted permission to write to that path.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.

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.GetDirectories(currentDir) 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="multipart/form-data" runat="server">
<FONT face=verdana>Select File to Upload:</FONT> <INPUT
id=uploadedFile type=file name=uploadedFile 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.Page
Dim currentDir As String
Dim directorySeparatorChar As Char = Path.DirectorySeparatorChar
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Protected WithEvents dirContent As System.Web.UI.WebControls.Label
Protected WithEvents message As System.Web.UI.WebControls.Label
Protected WithEvents uploadedFile As
System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents upload As System.Web.UI.HtmlControls.HtmlInputButton
Protected WithEvents NavMenu1 As NavMenu.CustomControls.NavMenu
'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) 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.NavMenuHeading = "Archive Tracking System"
Call InitNavMenu(NavMenu1)
' Dim root As String = "C:\Inetpub\wwwroot\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.StartsWith(root) Then
currentDir = root
End If
Dim sb As New System.Text.StringBuilder(4096)
Try
If Not currentDir.Equals(Root) Then
' not at the root
Dim currentDirParent As String
Dim lastIndex As Integer = _
currentDir.LastIndexOf(directorySeparatorChar)
If lastIndex <> -1 Then
currentDirParent = currentDir.Substring(0, lastIndex)
Else
currentDirParent = currentDir
End If
sb.Append("<a href=").Append(thisPage)
sb.Append("?dir=").Append(Server.UrlEncode(currentDirParent))
sb.Append("><img width=30 border=0 src=images/Up.gif></a><br>")
End If
sb.Append("<br><img border=0 src=images/OpenFolder.gif> ")
sb.Append("<font face=verdana>")
sb.Append(currentDir)
sb.Append("</font>")
sb.Append("<br>")
sb.Append("<table>")
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.Redirect("ErrorPage.aspx")
End Try
Try
Dim dirs() As String
dirs = Directory.GetDirectories(currentDir)
Dim d As String
For Each d In dirs
Dim dirName As String = Path.GetFileName(d)
sb.Append("<tr>")
sb.Append("<td><img src=images/Folder.gif> ")
sb.Append("<a href=").Append(thisPage)
sb.Append("?dir=").Append(Server.UrlEncode(currentDir))
sb.Append(directorySeparatorChar)
sb.Append(Server.UrlEncode(dirName))
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(Directory.GetLastWriteTime(currentDir & _
directorySeparatorChar.ToString() & dirName).ToString())
sb.Append("</font></td>")
sb.Append("</tr>")
Next
Catch ex As Exception
sException = ex
sErrorPage = "file upload"
Response.Redirect("ErrorPage.aspx")
End Try
Try
Dim dirInfo As New DirectoryInfo(currentDir)
Dim files() As FileInfo
files = dirInfo.GetFiles()
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=FileDownload.aspx?file=")
sb.Append(Server.UrlEncode(currentDir))
sb.Append(directorySeparatorChar)
sb.Append(Server.UrlEncode(filename))
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.Length.ToString())
sb.Append("</font></td>")
sb.Append("<td><font face=verdana size=2>")
sb.Append(File.GetLastWriteTime(currentDir & _
directorySeparatorChar.ToString() & f.Name).ToString())
sb.Append("</font></td>")
sb.Append("</tr>")
Next
Catch ex As Exception
sException = ex
sErrorPage = "file upload"
Response.Redirect("ErrorPage.aspx")
End Try
sb.Append("</table>")
dirContent.Text = sb.ToString()
End Sub
Private Sub upload_ServerClick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles upload.ServerClick
If Not (uploadedFile.PostedFile Is Nothing) Then
Try
Dim postedFile = uploadedFile.PostedFile
Dim filename As String = Path.GetFileName(postedFile.FileName)
Dim contentType As String = postedFile.ContentType
Dim contentLength As Integer = postedFile.ContentLength
postedFile.SaveAs(currentDir & directorySeparatorChar.ToString() &
filename)
Catch ex As Exception
sException = ex
sErrorPage = "file upload"
Response.Redirect("ErrorPage.aspx")
End Try
End If
End Sub
End Class

The error I get is this:
A system error has occurred at:
file upload

System.UnauthorizedAccessException: Access to the path
"\\les-net\les\Special Projects\ATSPDF" is denied. at
System.IO.__Error.WinIOError(Int32 errorCode, String str) at
System.IO.Directory.InternalGetFileDirectoryNames(String fullPath, String
userPath, Boolean file) at System.IO.Directory.InternalGetDirectories(String
path, String userPath, String searchPattern) at
System.IO.Directory.GetDirectories(String path, String searchPattern) at
System.IO.Directory.GetDirectories(String path) at
ATS.UploadPDFFiles.Page_Load(Object sender, EventArgs e) in
c:\inetpub\wwwroot\ATS\UploadPDFFiles.aspx.vb:line 87

Access to the path "\\les-net\les\Special Projects\ATSPDF" is denied.

Thanks for any help!
 
S

Steve C. Orr [MVP, MCSD]

The ASPNET user account that ASP.NET uses by default does not have network
permissions. Either give the account such network permissions (likely with
the help of your network administrator) or use impersonation to have it run
under a different user account that does have such permissions.
For testing purposes you can have it run under your personal user account.
Here's more info:
http://msdn.microsoft.com/library/d...-us/cpguide/html/cpconaspnetimpersonation.asp

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net

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.GetDirectories(currentDir) 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="multipart/form-data" runat="server">
<FONT face=verdana>Select File to Upload:</FONT> <INPUT
id=uploadedFile type=file name=uploadedFile 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.Page
Dim currentDir As String
Dim directorySeparatorChar As Char = Path.DirectorySeparatorChar
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents dirContent As System.Web.UI.WebControls.Label
Protected WithEvents message As System.Web.UI.WebControls.Label
Protected WithEvents uploadedFile As System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents upload As System.Web.UI.HtmlControls.HtmlInputButton
Protected WithEvents NavMenu1 As NavMenu.CustomControls.NavMenu
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) 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.NavMenuHeading = "Archive Tracking System"
Call InitNavMenu(NavMenu1)
' Dim root As String = "C:\Inetpub\wwwroot\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.StartsWith(root) Then
currentDir = root
End If
Dim sb As New System.Text.StringBuilder(4096)
Try
If Not currentDir.Equals(Root) Then
' not at the root
Dim currentDirParent As String
Dim lastIndex As Integer = _
currentDir.LastIndexOf(directorySeparatorChar)
If lastIndex <> -1 Then
currentDirParent = currentDir.Substring(0, lastIndex)
Else
currentDirParent = currentDir
End If
sb.Append("<a href=").Append(thisPage)
sb.Append("?dir=").Append(Server.UrlEncode(currentDirParent))
sb.Append("><img width=30 border=0 src=images/Up.gif></a><br>")
End If
sb.Append("<br><img border=0 src=images/OpenFolder.gif> ")
sb.Append("<font face=verdana>")
sb.Append(currentDir)
sb.Append("</font>")
sb.Append("<br>")
sb.Append("<table>")
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.Redirect("ErrorPage.aspx")
End Try
Try
Dim dirs() As String
dirs = Directory.GetDirectories(currentDir)
Dim d As String
For Each d In dirs
Dim dirName As String = Path.GetFileName(d)
sb.Append("<tr>")
sb.Append("<td><img src=images/Folder.gif> ")
sb.Append("<a href=").Append(thisPage)
sb.Append("?dir=").Append(Server.UrlEncode(currentDir))
sb.Append(directorySeparatorChar)
sb.Append(Server.UrlEncode(dirName))
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(Directory.GetLastWriteTime(currentDir & _
directorySeparatorChar.ToString() & dirName).ToString())
sb.Append("</font></td>")
sb.Append("</tr>")
Next
Catch ex As Exception
sException = ex
sErrorPage = "file upload"
Response.Redirect("ErrorPage.aspx")
End Try
Try
Dim dirInfo As New DirectoryInfo(currentDir)
Dim files() As FileInfo
files = dirInfo.GetFiles()
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=FileDownload.aspx?file=")
sb.Append(Server.UrlEncode(currentDir))
sb.Append(directorySeparatorChar)
sb.Append(Server.UrlEncode(filename))
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.Length.ToString())
sb.Append("</font></td>")
sb.Append("<td><font face=verdana size=2>")
sb.Append(File.GetLastWriteTime(currentDir & _
directorySeparatorChar.ToString() & f.Name).ToString())
sb.Append("</font></td>")
sb.Append("</tr>")
Next
Catch ex As Exception
sException = ex
sErrorPage = "file upload"
Response.Redirect("ErrorPage.aspx")
End Try
sb.Append("</table>")
dirContent.Text = sb.ToString()
End Sub
Private Sub upload_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles upload.ServerClick
If Not (uploadedFile.PostedFile Is Nothing) Then
Try
Dim postedFile = uploadedFile.PostedFile
Dim filename As String = Path.GetFileName(postedFile.FileName)
Dim contentType As String = postedFile.ContentType
Dim contentLength As Integer = postedFile.ContentLength
postedFile.SaveAs(currentDir & directorySeparatorChar.ToString() & filename)
Catch ex As Exception
sException = ex
sErrorPage = "file upload"
Response.Redirect("ErrorPage.aspx")
End Try
End If
End Sub
End Class

The error I get is this:
A system error has occurred at:
file upload

System.UnauthorizedAccessException: Access to the path "\\les-net\les\Special Projects\ATSPDF" is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String str) at System.IO.Directory.InternalGetFileDirectoryNames(String fullPath, String userPath, Boolean file) at System.IO.Directory.InternalGetDirectories(String path, String userPath, String searchPattern) at System.IO.Directory.GetDirectories(String path, String searchPattern) at System.IO.Directory.GetDirectories(String path) at ATS.UploadPDFFiles.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\ATS\UploadPDFFiles.aspx.vb:line 87

Access to the path "\\les-net\les\Special Projects\ATSPDF" is denied.

Thanks for any help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top