By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,498 Members | 1,564 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,498 IT Pros & Developers. It's quick & easy.

File Upload/Downloader

P: n/a
BW
I am creating an upload/download function for an extranet site. Files will
be uploaded to directory based upon the users login and associated project.
The function works as long as I use "c:\Temp" as the directory. When I use
any other hard coded directory or even Server.MapPath() the upload function
fails and returns the error: "Exception has been thrown by the target of an
invocation." Once I change the root directory to "c:\Temp", everything works
as it should. I've checked permissions, etc.

Any ideas? I have included the entire page/codebehind. The sub causing the
error is "DoUpload()".

Thanks in advance

Bernard

================================================== ================================================== ================
FMU1.aspx
================================================== ================================================== ================
<%@ Page Language="vb" AutoEventWireup="true" Codebehind="FMU1.aspx.vb"
Inherits="FileManagementUtility.aaLoader"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>aa - File Management Utility</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" encType="multipart/form-data" runat="server">
<P><asp:label id="dirContent" runat="server"></asp:label><asp:label
id="message" runat="server"></asp:label></P>
<p>
<hr>
<%-- File Upload --%>
<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" OnServerClick="Page_Load">
<p>
</form>
</P>
</body>
</HTML>
'================================================= ================================================== ================
'================================================= ================================================== ================
'CodeBehind
'FMU1.aspx.vb
'================================================= ================================================== ================
Imports System
Imports System.IO
Imports System.Text

Public Class aaLoader
Inherits System.Web.UI.Page

#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

'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

Dim currentDir As String
Dim directorySeparatorChar As Char = Path.DirectorySeparatorChar

Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

If Not IsPostBack() Then

Dim root As String = Server.MapPath("Projects") '"C:\temp"
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 StringBuilder(4096)

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(current DirParent))
sb.Append("><img width=30 border=0
src=images/Up.gif></a><br>")
End If

DoUpload()

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>File
Name</font></td>")
sb.Append("<td><font face=verdana size=3>File Type</font></td>")
sb.Append("<td><font face=verdana size=3>File Size</font></td>")
sb.Append("<td><font face=verdana size=3>Last
Modified</font></td>")
sb.Append("</tr>")
Dim dirs() As String
Try
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(current Dir))
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 & "\" &
dirName).ToString())
sb.Append(Directory.GetLastWriteTime(currentDir & _
directorySeparatorChar.ToString() &
dirName).ToString())
sb.Append("</font></td>")
sb.Append("</tr>")
Next

Catch ex As Exception
message.Text = ex.Message.ToString
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
message.Text = ex.Message.ToString
End Try

sb.Append("</table>")
dirContent.Text = sb.ToString()
Else
Dim root As String = Server.MapPath("Projects") '"C:\temp"
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 StringBuilder(4096)

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(current DirParent))
sb.Append("><img width=30 border=0
src=images/Up.gif></a><br>")
End If

DoUpload()

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>File
Name</font></td>")
sb.Append("<td><font face=verdana size=3>File Type</font></td>")
sb.Append("<td><font face=verdana size=3>File Size</font></td>")
sb.Append("<td><font face=verdana size=3>Last
Modified</font></td>")
sb.Append("</tr>")
Dim dirs() As String
Try
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(current Dir))
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 & "\" &
dirName).ToString())
sb.Append(Directory.GetLastWriteTime(currentDir & _
directorySeparatorChar.ToString() &
dirName).ToString())
sb.Append("</font></td>")
sb.Append("</tr>")
Next

Catch ex As Exception
message.Text = ex.Message.ToString
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
message.Text = ex.Message.ToString
End Try

sb.Append("</table>")
dirContent.Text = sb.ToString()
End If

End Sub

Sub DoUpload()

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
message.Text = "Failed uploading file because: " &
ex.Message.ToString
End Try
End If
End Sub
End Class
Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
BW
It was permissions...

"BW" <bw*********@aegisadvisers.com> wrote in message
news:eL*************@TK2MSFTNGP10.phx.gbl...
I am creating an upload/download function for an extranet site. Files will
be uploaded to directory based upon the users login and associated project.
The function works as long as I use "c:\Temp" as the directory. When I use
any other hard coded directory or even Server.MapPath() the upload function
fails and returns the error: "Exception has been thrown by the target of an
invocation." Once I change the root directory to "c:\Temp", everything
works as it should. I've checked permissions, etc.

Any ideas? I have included the entire page/codebehind. The sub causing the
error is "DoUpload()".

Thanks in advance

Bernard

================================================== ================================================== ================
FMU1.aspx
================================================== ================================================== ================
<%@ Page Language="vb" AutoEventWireup="true" Codebehind="FMU1.aspx.vb"
Inherits="FileManagementUtility.aaLoader"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>aa - File Management Utility</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" encType="multipart/form-data" runat="server">
<P><asp:label id="dirContent" runat="server"></asp:label><asp:label
id="message" runat="server"></asp:label></P>
<p>
<hr>
<%-- File Upload --%>
<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" OnServerClick="Page_Load">
<p>
</form>
</P>
</body>
</HTML>
'================================================= ================================================== ================
'================================================= ================================================== ================
'CodeBehind
'FMU1.aspx.vb
'================================================= ================================================== ================
Imports System
Imports System.IO
Imports System.Text

Public Class aaLoader
Inherits System.Web.UI.Page

#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

'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

Dim currentDir As String
Dim directorySeparatorChar As Char = Path.DirectorySeparatorChar

Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

If Not IsPostBack() Then

Dim root As String = Server.MapPath("Projects") '"C:\temp"
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 StringBuilder(4096)

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(current DirParent))
sb.Append("><img width=30 border=0
src=images/Up.gif></a><br>")
End If

DoUpload()

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>File
Name</font></td>")
sb.Append("<td><font face=verdana size=3>File
Type</font></td>")
sb.Append("<td><font face=verdana size=3>File
Size</font></td>")
sb.Append("<td><font face=verdana size=3>Last
Modified</font></td>")
sb.Append("</tr>")
Dim dirs() As String
Try
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(current Dir))
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 & "\"
& dirName).ToString())
sb.Append(Directory.GetLastWriteTime(currentDir & _
directorySeparatorChar.ToString() &
dirName).ToString())
sb.Append("</font></td>")
sb.Append("</tr>")
Next

Catch ex As Exception
message.Text = ex.Message.ToString
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
message.Text = ex.Message.ToString
End Try

sb.Append("</table>")
dirContent.Text = sb.ToString()
Else
Dim root As String = Server.MapPath("Projects") '"C:\temp"
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 StringBuilder(4096)

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(current DirParent))
sb.Append("><img width=30 border=0
src=images/Up.gif></a><br>")
End If

DoUpload()

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>File
Name</font></td>")
sb.Append("<td><font face=verdana size=3>File
Type</font></td>")
sb.Append("<td><font face=verdana size=3>File
Size</font></td>")
sb.Append("<td><font face=verdana size=3>Last
Modified</font></td>")
sb.Append("</tr>")
Dim dirs() As String
Try
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(current Dir))
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 & "\"
& dirName).ToString())
sb.Append(Directory.GetLastWriteTime(currentDir & _
directorySeparatorChar.ToString() &
dirName).ToString())
sb.Append("</font></td>")
sb.Append("</tr>")
Next

Catch ex As Exception
message.Text = ex.Message.ToString
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
message.Text = ex.Message.ToString
End Try

sb.Append("</table>")
dirContent.Text = sb.ToString()
End If

End Sub

Sub DoUpload()

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
message.Text = "Failed uploading file because: " &
ex.Message.ToString
End Try
End If
End Sub
End Class

Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.