Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > File Upload/Downloader

Reply
Thread Tools

File Upload/Downloader

 
 
BW
Guest
Posts: n/a
 
      03-04-2005
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


 
Reply With Quote
 
 
 
 
BW
Guest
Posts: n/a
 
      03-09-2005
It was permissions...

"BW" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting JPG file ppt file [Powerpoint] file zxcvar Digital Photography 7 06-22-2009 07:54 PM
Reading of file by next of map file and by next of file descriptor. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki C++ 1 07-10-2007 02:46 AM
How to create MDF file (.mdf) file from XML file. Dave ASP .Net 1 06-07-2007 11:32 PM
In file parsing, taking the first few characters of a text file after a readfile or streamreader file read... .Net Sports ASP .Net 11 01-17-2006 12:44 AM
An Automated process of watching a network file folder, reading a file in it and deleting the file using ASP.NET ? Luis Esteban Valencia Muņoz ASP .Net 3 06-04-2005 10:56 AM



Advertisments