Thanks Cerebrus!  I haven't made use of FileStream and MemoryStream
much in the past - I changed from using the filestream in my function
to using memorystream.  I haven't tested it a lot yet, but so far
seems to be working fine.  Thanks for the help.


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


On Oct 23, 3:28 am, Cerebrus <[email protected]> wrote:
> On Oct 22, 10:03 pm, Sheldon <[email protected]> wrote:
>
> > 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.
>
> A FileStream *is* a Stream of bytes wrapped around a File on the disk.
> They are not mutually exclusive objects.
>
> > 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.
>
> Use a MemoryStream instead of a FileStream.

Reply via email to