473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

FTP Class Improvement Help Needed

I have been using this FTP class for ever and it has worked great
until recently. Normally I'm only downloading files that range in
size from 1-2 meg in size. Now I have a file that is 1.2 gigs and I'm
getting the system out of memory error. Is there a way to stream
directly to a file instead of memory?

Imports System.IO
Imports System.Net
Imports System.Text

Public Class clsftp
Public Function Download(ByVal destinationFile As String, ByVal
downloadUri As Uri) As FtpStatusCode

Try
' Check if the URI is and FTP site
If Not (downloadUri.Scheme = Uri.UriSchemeFtp) Then
Throw New ArgumentException("URI is not an FTp site")
End If

' Set up the request
Dim ftpRequest As FtpWebRequest =
CType(WebRequest.Create(downloadUri), FtpWebRequest)

' use the provided credentials
If Me.m_isAnonymousUser = False Then
ftpRequest.Credentials = New
NetworkCredential(Me.m_userName, Me.m_password)
End If

' Download a file. Look at the other methods to see all of
the potential FTP features
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile

' get the response object
Dim ftpResponse As FtpWebResponse =
CType(ftpRequest.GetResponse, FtpWebResponse)
Dim stream As Stream = Nothing
Dim reader As StreamReader = Nothing
Dim writer As StreamWriter = Nothing

' get the file as a stream from the response object and
write it as
' a file stream to the local PC
Try
stream = ftpResponse.GetResponseStream
reader = New StreamReader(stream, Encoding.UTF8)
writer = New StreamWriter(destinationFile, False)
writer.Write(reader.ReadToEnd)
Return ftpResponse.StatusCode
Finally
' Allways close all streams
stream.Close()
reader.Close()
writer.Close()
End Try
Catch ex As Exception
Throw ex
End Try

End Function

Public Property UserName() As String
Get
Return Me.m_userName
End Get
Set(ByVal value As String)
Me.m_userName = value
End Set
End Property

Public Property Password() As String
Get
Return Me.m_password
End Get
Set(ByVal value As String)
Me.m_password = value
End Set
End Property

Public Property IsAnonymousUser() As Boolean
Get
Return Me.m_isAnonymousUser
End Get
Set(ByVal value As Boolean)
Me.m_isAnonymousUser = value
End Set
End Property

Private m_userName As String
Private m_password As String
Private m_isAnonymousUser As Boolean
End Class

May 17 '07 #1
1 1226
try stream in chunks:

....
Dim writer As FileStream = Nothing
Dim readCount As Integer
Dim buffer(5119) As Byte '5K buffer = (5 * 1024)-1

writer = New FileStream(destinationFile, FileMode.Create)
Do
readCount = ftpStream.Read(buffer, 0, buffer.Length)
writer.Write(buffer, 0, readCount)
Loop Until (readCount = 0)

writer.Flush()
....

"pmclinn" wrote:
I have been using this FTP class for ever and it has worked great
until recently. Normally I'm only downloading files that range in
size from 1-2 meg in size. Now I have a file that is 1.2 gigs and I'm
getting the system out of memory error. Is there a way to stream
directly to a file instead of memory?

Imports System.IO
Imports System.Net
Imports System.Text

Public Class clsftp
Public Function Download(ByVal destinationFile As String, ByVal
downloadUri As Uri) As FtpStatusCode

Try
' Check if the URI is and FTP site
If Not (downloadUri.Scheme = Uri.UriSchemeFtp) Then
Throw New ArgumentException("URI is not an FTp site")
End If

' Set up the request
Dim ftpRequest As FtpWebRequest =
CType(WebRequest.Create(downloadUri), FtpWebRequest)

' use the provided credentials
If Me.m_isAnonymousUser = False Then
ftpRequest.Credentials = New
NetworkCredential(Me.m_userName, Me.m_password)
End If

' Download a file. Look at the other methods to see all of
the potential FTP features
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile

' get the response object
Dim ftpResponse As FtpWebResponse =
CType(ftpRequest.GetResponse, FtpWebResponse)
Dim stream As Stream = Nothing
Dim reader As StreamReader = Nothing
Dim writer As StreamWriter = Nothing

' get the file as a stream from the response object and
write it as
' a file stream to the local PC
Try
stream = ftpResponse.GetResponseStream
reader = New StreamReader(stream, Encoding.UTF8)
writer = New StreamWriter(destinationFile, False)
writer.Write(reader.ReadToEnd)
Return ftpResponse.StatusCode
Finally
' Allways close all streams
stream.Close()
reader.Close()
writer.Close()
End Try
Catch ex As Exception
Throw ex
End Try

End Function

Public Property UserName() As String
Get
Return Me.m_userName
End Get
Set(ByVal value As String)
Me.m_userName = value
End Set
End Property

Public Property Password() As String
Get
Return Me.m_password
End Get
Set(ByVal value As String)
Me.m_password = value
End Set
End Property

Public Property IsAnonymousUser() As Boolean
Get
Return Me.m_isAnonymousUser
End Get
Set(ByVal value As Boolean)
Me.m_isAnonymousUser = value
End Set
End Property

Private m_userName As String
Private m_password As String
Private m_isAnonymousUser As Boolean
End Class

May 18 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Ron_Adam | last post by:
Hi, Thanks again for all the helping me understand the details of decorators. I put together a class to create decorators that could make them a lot easier to use. It still has a few glitches...
15
by: Mon | last post by:
I am in the process of reorganizing my code and came across and I came across a problem, as described in the subject line of this posting. I have many classes that have instances of other classes...
822
by: Turamnvia Suouriviaskimatta | last post by:
I 'm following various posting in "comp.lang.ada, comp.lang.c++ , comp.realtime, comp.software-eng" groups regarding selection of a programming language of C, C++ or Ada for safety critical...
4
by: Steve Jorgensen | last post by:
In response to recent questions on when to use class modules in Access VBA, here's a real-world scenario of mine. I had some code that needed to build a Where clause for a Select statement based...
11
by: dimension | last post by:
If my intentions are to create objects that encapsulates data rows in a table, is it better to use a Struct or Class? Based on what i read, if my objects will simply have get and set methods,...
4
by: d0wsdkn02 | last post by:
I need some architecture help. Our app is similar between clients, but every client has specific needs that can require us to change anything. I'll concentrate on one class below, but potentially...
2
by: Fish | last post by:
I have been researching the correct way to organize my solution so that it makes best use of VB.NET inherent ability to manage resources such as objects. My solution contains 2 projects and the...
7
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9...
2
by: nw | last post by:
Hi, I previously posted a question asking for suggestions on my class design. My original code consisted of a pure virtual base class Body which contained a number of integration algorithms...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.