Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FTP download to FileStream

Status
Not open for further replies.

stroyer74

IS-IT--Management
Jan 22, 2003
70
US
I am working on a function that will download a file from an specified FTP site and return the file as a filestream. I have found a number of very helpful posts online that have gotten me quite a ways on it. I'm able to get the file downloaded, but am drawing a blank as to how to download it as a filestream instead of a physical file on the user's hard drive.

Using the code listed below, I'd like to not create the file "C:\temp\temp.123", but rather return the file as a filestream.

Can anyone give me some pointers on this? Maybe I'm going about it the wrong way. Ultimately what I will be doing is downloading a file from the FTP site, passing it to a file viewer program for display. Once the user closes the viewer, the download file should be discarded. If I am dealing with physical files on the user's hard drive, then I also have to make sure my file viewer deletes the downloaded file before closing; I figured it would be easier to just pass the downloaded file around as an object in memory.

Here is the function as it stands right now:

----------------------
Public Function ftpDownload(ByVal strFileLocation As String, ByVal strFilename As String, _
ByVal strServer As String, ByVal strUsername As String, ByVal strPassword As String) As FileStream

Try
Dim ftp As FtpWebRequest = CType(FtpWebRequest.Create(strServer & strFileLocation & strFilename), FtpWebRequest)
ftp.Credentials = New NetworkCredential(strUsername, strPassword)
ftp.KeepAlive = False
ftp.UseBinary = True
ftp.Method = WebRequestMethods.Ftp.DownloadFile

Using response As FtpWebResponse = CType(ftp.GetResponse, FtpWebResponse)
Using responseStream As IO.Stream = response.GetResponseStream
Using fs As New IO.FileStream("c:\temp\temp.123", FileMode.Create)
Dim buffer(2047) As Byte
Dim read As Integer = 0
Do
read = responseStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, read)
Loop Until read = 0
responseStream.Close()
fs.Flush()
fs.Close()
End Using
responseStream.Close()
End Using
response.Close()
End Using


Catch ex As Exception
MsgBox(ex)
End Try
End Function


----------------------

Any ideas? Thanks,
Sheldon
 
I can't help you with the ftp file stream other than to give you a warning that generally that is going to be a bad idea. The biggest problem is you are eating up memory unless you are controlling the file sizes to fairly small. How much memory is the user guaranteed to have? If you exceed that you are just forcing their system to save to the page file which is just on the hard disk any way. Much better to just go ahead and download the file then remove it if they don't want it. It is always better to clean up your own mess rather than risk causing problems by leaving it behind.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Sorwen, I can appreciate your warning. That is something that I will have to watch closely - limit the number or viewers open at once, limit the size of the files, etc. But in this case, it will only be used within the my company so I can feel more certain what specs the user will have. Some files could possibly be sensitive and I really don't want to have to worry about those files being left sitting in the temp folder (if the program didn't close gracefully) waiting for someone else to find.

Anyhow, I was able to find a solution. I needed to download the file to a memorystream instead of a filestream. I haven't tested the function extensively yet, but so far it has worked very well.


---

Public Class WebFunctions

Public Function ftpDownload(ByVal strFileLocation As String, ByVal strFilename As String, _
ByVal strServer As String, ByVal strUsername As String, ByVal strPassword As String) As MemoryStream
Dim ms As MemoryStream
Try
Dim ftp As FtpWebRequest = CType(FtpWebRequest.Create(strServer & strFileLocation & strFilename), FtpWebRequest)
ftp.Credentials = New NetworkCredential(strUsername, strPassword)
ftp.KeepAlive = False
ftp.UseBinary = True
ftp.Method = WebRequestMethods.Ftp.DownloadFile

Using response As FtpWebResponse = CType(ftp.GetResponse, FtpWebResponse)
Using responseStream As IO.Stream = response.GetResponseStream
Using msTemp As New IO.MemoryStream()
Dim buffer(2047) As Byte
Dim read As Integer = 0
Do
read = responseStream.Read(buffer, 0, buffer.Length)
msTemp.Write(buffer, 0, read)
Loop Until read = 0
responseStream.Close()
ms = New MemoryStream
ms.Write(msTemp.ToArray, 0, msTemp.Length)

msTemp.Flush()
msTemp.Close()
End Using
responseStream.Close()
End Using
response.Close()
End Using

Return ms

Catch ex As Exception
MsgBox(ex)
End Try
End Function
 
If you are able to limit without problems and know the specs then you should be fine. Your other option would be to encrypt the files with some sort of session key, but that could get overly complicated. Thanks for posting the resolution back. While I've not had to do something like that it is nice to know how just incase as it could come up. :)

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top