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: http://dotnetdevelopment.pastebin.com/m69fbf8de 1. Public Function ftpDownload(ByVal strFileLocation As String, ByVal strFilename As String, _ 2. ByVal strServer As String, ByVal strUsername As String, ByVal strPassword As String) As FileStream 3. 4. Try 5. Dim ftp As FtpWebRequest = CType(FtpWebRequest.Create (strServer & strFileLocation & strFilename), FtpWebRequest) 6. ftp.Credentials = New NetworkCredential(strUsername, strPassword) 7. ftp.KeepAlive = False 8. ftp.UseBinary = True 9. ftp.Method = WebRequestMethods.Ftp.DownloadFile 10. 11. Using response As FtpWebResponse = CType (ftp.GetResponse, FtpWebResponse) 12. Using responseStream As IO.Stream = response.GetResponseStream 13. Using fs As New IO.FileStream("c:\temp\temp. 123", FileMode.Create) 14. Dim buffer(2047) As Byte 15. Dim read As Integer = 0 16. Do 17. read = responseStream.Read(buffer, 0, buffer.Length) 18. fs.Write(buffer, 0, read) 19. Loop Until read = 0 20. responseStream.Close() 21. fs.Flush() 22. fs.Close() 23. End Using 24. responseStream.Close() 25. End Using 26. response.Close() 27. End Using 28. 29. 30. Catch ex As Exception 31. MsgBox(ex) 32. End Try 33. End Function Any ideas? Thanks, Sheldon
