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
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