Saving a stream to a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • K Viltersten

    Saving a stream to a file

    I've downloaded a file from an FTP server and
    using WebRequestMetho ds.Ftp.Download File and the
    result is, i guess, a file. However, the response is
    of type FtpWebResponse and as such only have a method
    for obtaining a Stream object only...

    I'd like it to respond with an object of type File or
    FileInfo, maybe, directly. Is it doable? Or is the
    best way to do that simply using FileWriter?

    --
    Regards
    Konrad Viltersten
    ----------------------------------------
    May all spammers die an agonizing death;
    have no burial places; their souls be
    chased by demons in Gehenna from one room
    to another for all eternity and beyond.


  • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

    #2
    Re: Saving a stream to a file

    K Viltersten wrote:
    I've downloaded a file from an FTP server and
    using WebRequestMetho ds.Ftp.Download File and the
    result is, i guess, a file. However, the response is
    of type FtpWebResponse and as such only have a method
    for obtaining a Stream object only...
    >
    I'd like it to respond with an object of type File or
    FileInfo, maybe, directly. Is it doable?
    It has the methods it ha.

    WebClient has some convenience methods like DownloadFile.

    Arne

    Comment

    • K Viltersten

      #3
      Re: Saving a stream to a file

      >I've downloaded a file from an FTP server and
      >using WebRequestMetho ds.Ftp.Download File and the
      >result is, i guess, a file. However, the response is
      >of type FtpWebResponse and as such only have a method
      >for obtaining a Stream object only...
      >>
      >I'd like it to respond with an object of type File or
      >FileInfo, maybe, directly. Is it doable?
      >
      It has the methods it ha.
      >
      WebClient has some convenience methods like DownloadFile.
      Right, thanks! I was hoping for a neat one-liner
      doing everything automagically for me. :)

      Perhaps i'm getting too convenient, hehe.


      --
      Regards
      Konrad Viltersten
      ----------------------------------------
      May all spammers die an agonizing death;
      have no burial places; their souls be
      chased by demons in Gehenna from one room
      to another for all eternity and beyond.


      Comment

      • The Colorado Kid

        #4
        Re: Saving a stream to a file

        Hello K,

        Here ya go:

        Public Function Download(ByVal RemoteFile As String, ByVal LocalFile
        As String) As Boolean
        Try
        Dim ftp As FtpWebRequest = GetRequest(GetU RI(RemoteFile))
        ftp.Method = WebRequestMetho ds.Ftp.Download File

        Using response As System.Net.FtpW ebResponse = CType(ftp.GetRe sponse,
        System.Net.FtpW ebResponse)
        Using responseStream As IO.Stream = response.GetRes ponseStream
        Using fs As New IO.FileStream(L ocalFile, IO.FileMode.Cre ate)
        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
        _error = ex
        Return False
        End Try
        Return True
        End Function

        I've downloaded a file from an FTP server and
        using WebRequestMetho ds.Ftp.Download File and the
        result is, i guess, a file. However, the response is
        of type FtpWebResponse and as such only have a method
        for obtaining a Stream object only...
        I'd like it to respond with an object of type File or FileInfo, maybe,
        directly. Is it doable? Or is the best way to do that simply using
        FileWriter?
        >

        Comment

        • Peter Duniho

          #5
          Re: Saving a stream to a file

          On Thu, 31 Jul 2008 14:01:01 -0700, The Colorado Kid <tckwrote:
          Hello K,
          >
          Here ya go: [VB code snipped]
          Uh...I don't mean to be a bother, but traditionally here in the C#
          newsgroup, we try to stick to posting C# code. :)

          Comment

          • The Colorado Kid

            #6
            Re: Saving a stream to a file

            Hello Peter,

            Aha! Helps if I pay attention to which newsgroup I'm pulling articles from.
            Sorry about that. I've got a slew of newsgroups that all come up in the same
            stream. I'll pay closer attention in the future.

            Ironically, I had to convert that code from C# to VB for the app it's used
            in...
            On Thu, 31 Jul 2008 14:01:01 -0700, The Colorado Kid <tckwrote:
            >
            >Hello K,
            >>
            >Here ya go: [VB code snipped]
            >>
            Uh...I don't mean to be a bother, but traditionally here in the C#
            newsgroup, we try to stick to posting C# code. :)
            >

            Comment

            • K Viltersten

              #7
              Re: Saving a stream to a file

              Ironically, I had to convert that code from C# to VB for the app it's used
              in...
              How about reposting it in the
              before-conversion state, then?

              --
              Regards
              Konrad Viltersten
              ----------------------------------------
              May all spammers die an agonizing death;
              have no burial places; their souls be
              chased by demons in Gehenna from one room
              to another for all eternity and beyond.


              Comment

              • Peter Duniho

                #8
                Re: Saving a stream to a file

                On Fri, 01 Aug 2008 00:12:16 -0700, The Colorado Kid <tckwrote:
                Hello Peter,
                >
                Aha! Helps if I pay attention to which newsgroup I'm pulling articles
                from. Sorry about that. I've got a slew of newsgroups that all come up
                in the same stream. I'll pay closer attention in the future.
                It's not a big deal. I just thought it was funny. Heck, I'm the one who
                answered a C# question here as if it were about Java just the other day.
                I know how hard it can be to keep your newsgroups straight.
                Ironically, I had to convert that code from C# to VB for the app it's
                used in...
                Yes, that is ironic. :) As Konrad says, maybe it would help to post the
                original C# then?

                Pete

                Comment

                • The Colorado Kid

                  #9
                  Re: Saving a stream to a file

                  Peter,

                  I can't find the original source, but here's something similar:


                  On Fri, 01 Aug 2008 00:12:16 -0700, The Colorado Kid <tckwrote:
                  >
                  >Hello Peter,
                  >>
                  >Aha! Helps if I pay attention to which newsgroup I'm pulling articles
                  >from. Sorry about that. I've got a slew of newsgroups that all come
                  >up in the same stream. I'll pay closer attention in the future.
                  >>
                  It's not a big deal. I just thought it was funny. Heck, I'm the one
                  who answered a C# question here as if it were about Java just the
                  other day. I know how hard it can be to keep your newsgroups
                  straight.
                  >
                  >Ironically, I had to convert that code from C# to VB for the app it's
                  >used in...
                  >>
                  Yes, that is ironic. :) As Konrad says, maybe it would help to post
                  the original C# then?
                  >
                  Pete
                  >

                  Comment

                  Working...