I have created a program in ASP.Net and VB.Net that allows users to loging and download and upload files to and from an FTP site.
I have so far got it to the point where the user can download files and when they go to the download page, a listbox displays a list of the files that reside on the FTP site.
When I highlight a file and click download, the file downloads as required but the list is then duplicated by double, so you see each file listed twice.
Every time you then click on the download or delete button, the duplicates again and again.
Can anybody advise on why this is happening???
My code is below:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="OutDirectory.aspx.vb" Inherits="FTPUploadSite.OutDirectory" %>
<%@ Import Namespace="FTPUploadSite" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body style="height: 769px; width: 1630px; background-color: #99CCFF; margin-top: 0px;"
bgcolor="#99ffcc">
<form id="form1" runat="server">
<div style="text-align: center; font-family: Arial; font-size: xx-large; margin-top: 0px; height: 1072px;">
<br />
FTP Out Directory<br />
<br />
<br />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="ftp://10.9.16.80/dwinn/In"
style="font-size: small; font-weight: 700; color: #000000;">View FTP Files</asp:HyperLink>
<br />
<br />
<asp:ListBox ID="ListBox1" runat="server" Height="513px" Width="1111px"
BackColor="#99CCFF"></asp:ListBox>
<br />
<asp:Button ID="Button5" runat="server" Text="Download File" />
<asp:Button ID="Button6" runat="server" Text="Delete File" />
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Back to FTP Directories"
Width="167px" />
<br />
<br />
<asp:Button ID="Button2" runat="server" Text="Exit the system" Width="165px" />
<script runat="server" >
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim ftp As FtpWebRequest = DirectCast(WebRequest.Create("ftp://xx.xx.x.xx/In/"), FtpWebRequest)
ftp.Method = WebRequestMethods.Ftp.ListDirectory
Dim ftpFiles As New ArrayList()
ftp.Credentials = New NetworkCredential("user", "password")
Dim Response As FtpWebResponse = ftp.GetResponse()
Dim responseStream As Stream = Response.GetResponseStream()
Dim reader = New StreamReader(responseStream)
While Not (reader.EndOfStream)
ftpFiles.Add(reader.ReadLine())
'ListBox1.Items.Add(ftpFiles.ToString())
End While
For Each file In ftpFiles
ListBox1.Items.Add(file)
Next
reader.Close()
responseStream.Close()
Response.Close()
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Response.Redirect("NextPage.aspx")
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
Response.Redirect("NextPage.aspx")
End Sub
Protected Sub Button6_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button6.Click
Dim ftp As FtpWebRequest = DirectCast(FtpWebRequest.Create("ftp://xx.xx.x.xx/In/" & ListBox1.Text), FtpWebRequest)
If ListBox1.Text = "" Then
MsgBox("A file needs to be selected...!")
End If
If ListBox1.Text <> "" Then
Try
ftp.Credentials = New System.Net.NetworkCredential("user", "password")
ftp.Method = WebRequestMethods.Ftp.DeleteFile
Dim ftpResponse As FtpWebResponse = CType(ftp.GetResponse(), FtpWebResponse)
ftpResponse = ftp.GetResponse()
MsgBox(ftpResponse.StatusCode, ftpResponse.StatusDescription)
ftpResponse.Close()
Catch ex As Exception
End Try
End If
End Sub
Protected Sub Button5_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button5.Click
Dim ftp As FtpWebRequest = DirectCast(FtpWebRequest.Create("ftp://xx.xx.x.xx/In/" & ListBox1.Text), FtpWebRequest)
If ListBox1.Text = "" Then
MsgBox("A file needs to be selected...!")
End If
If ListBox1.Text <> "" Then
Try
ftp.Credentials = New System.Net.NetworkCredential("user", "password")
Dim ftpResponse As FtpWebResponse = ftp.GetResponse()
Dim ftpStream As Stream = ftpResponse.GetResponseStream()
Dim ftpFile As String = ListBox1.Text
Dim ftpFileOutputStream As New FileStream("C:\" & ListBox1.Text, FileMode.Create)
Dim ftpContentLength As Long = ftpResponse.ContentLength()
Dim ftpBufferSize As Integer = 2048
Dim ftpRead As Integer
Dim ftpBuffer(ftpBufferSize) As Byte
ftpRead = ftpStream.Read(ftpBuffer, 0, ftpBufferSize)
While (ftpRead > 0)
ftpFileOutputStream.Write(ftpBuffer, 0, ftpRead)
ftpRead = ftpStream.Read(ftpBuffer, 0, ftpBufferSize)
End While
ftpResponse.Close()
ftpStream.Close()
ftpFileOutputStream.Close()
Catch ex As Exception
End Try
End If
End Sub
</script>
</div>
</form>
</body>
</html>
Any help and advice will be great.
Thanks,
Dan