Download binary file (http)

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

    #1

    Download binary file (http)

    How can I download a binary file (like .doc or .pdf) from http to my
    harddrive using the .net framework?

    WebRequest objRequest = System.Net.Http WebRequest.Crea te(url);
    objResponse = objRequest.GetR esponse();
    BinaryReader bsRead = new BinaryReader(ob jResponse.GetRe sponseStream()
    FileStream fs = new FileStream("tes t.doc", FileMode.Create New);
    while(bsRead.Pe ekChar() != -1)
    {
    cbuf=bsRead.Rea dByte();
    fs.WriteByte(cb uf);
    }

    is one of the method I've tried. All of them failed becuase seeing in
    the base stream wasn't supported. All tutorals and examples i've seen,
    both on the internet and in msdn, deals with reading text, or a binary
    file of predefined size.

    Downloading a .doc file using Webclient.Downl oadFile(...) resulted in
    a file that seemed to have been treated like text, not binary.

    Please help!
  • Jon Skeet [C# MVP]

    #2
    Re: Download binary file (http)

    Fredrik <eelector@hotma il.com> wrote:[color=blue]
    > How can I download a binary file (like .doc or .pdf) from http to my
    > harddrive using the .net framework?
    >
    > WebRequest objRequest = System.Net.Http WebRequest.Crea te(url);
    > objResponse = objRequest.GetR esponse();
    > BinaryReader bsRead = new BinaryReader(ob jResponse.GetRe sponseStream()
    > FileStream fs = new FileStream("tes t.doc", FileMode.Create New);
    > while(bsRead.Pe ekChar() != -1)
    > {
    > cbuf=bsRead.Rea dByte();
    > fs.WriteByte(cb uf);
    > }
    >
    > is one of the method I've tried. All of them failed becuase seeing in
    > the base stream wasn't supported. All tutorals and examples i've seen,
    > both on the internet and in msdn, deals with reading text, or a binary
    > file of predefined size.[/color]

    Why are you creating the BinaryReader? Just read from the stream and
    write to your output stream:

    WebRequest objRequest = System.Net.Http WebRequest.Crea te(url);
    objResponse = objRequest.GetR esponse();
    byte[] buffer = new byte[32768];
    using (Stream input = objResponse.Get ResponseStream( ))
    {
    using (FileStream output = new FileStream ("test.doc",
    FileMode.Create New))
    {
    int bytesRead;

    while ( (bytesRead=inpu t.Read (buffer, 0, buffer.Length)) > 0)
    {
    output.Write(bu ffer, 0, bytesRead);
    }
    }
    }

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Madhu [MVP]

      #3
      Download binary file (http)

      Hi,

      The base stream which is returned by using
      GetResponseStre am doesn't support Seeking. Since it
      doesn't support Seeking, PeekChar method always returns -
      1. You can use the the Read method of the stream to read
      the contents of the stream.

      sample code snippet for doing the same is given below,

      byte[] buffer = new byte[4096];
      int length;

      length = respStream.Read (buffer,0,4096) ;
      while ( length > 0)
      {
      fs.Write(buffer ,0,length);
      length = respStream.Read (buffer,0,4096) ;
      }
      fs.Close();

      Hope this helps...

      Regards,
      Madhu

      MVP | MCSD.NET
      [color=blue]
      >-----Original Message-----
      >How can I download a binary file (like .doc or .pdf)[/color]
      from http to my[color=blue]
      >harddrive using the .net framework?
      >
      >WebRequest objRequest = System.Net.Http WebRequest.Crea te[/color]
      (url);[color=blue]
      >objResponse = objRequest.GetR esponse();
      >BinaryReader bsRead = new BinaryReader[/color]
      (objResponse.Ge tResponseStream ()[color=blue]
      >FileStream fs = new FileStream("tes t.doc",[/color]
      FileMode.Create New);[color=blue]
      >while(bsRead.P eekChar() != -1)
      >{
      > cbuf=bsRead.Rea dByte();
      > fs.WriteByte(cb uf);
      >}
      >
      >is one of the method I've tried. All of them failed[/color]
      becuase seeing in[color=blue]
      >the base stream wasn't supported. All tutorals and[/color]
      examples i've seen,[color=blue]
      >both on the internet and in msdn, deals with reading[/color]
      text, or a binary[color=blue]
      >file of predefined size.
      >
      >Downloading a .doc file using Webclient.Downl oadFile[/color]
      (...) resulted in[color=blue]
      >a file that seemed to have been treated like text, not[/color]
      binary.[color=blue]
      >
      >Please help!
      >.
      >[/color]

      Comment

      • seaweed

        #4
        Are there somebody build an OPC client by C#

        Hi,
        Could you give me some advices? I just begin the work. I don't know how to
        use RCW. I want to make a custom interface. I know the automation Interface
        need proxy-stub dll and automation wrapper, and then code the application
        program. how about custom interface? only proxy-stub dll is ok? and then
        build the custom interface. as for the proxy-stub dll, is there any
        difference between different company's OPC sever. or can I use a sample
        proxy-stub dll of OPC foundation to build a special custom interface?

        Thank you
        Best regards
        seaweed


        Comment

        • Morten Wennevik

          #5
          Re: Download binary file (http)


          WebClient client = new WebClient();
          client.Download File(url, saveasfilename) ;
          client.Dispose( );

          --
          Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

          Comment

          • Nicholas Paldino [.NET/C# MVP]

            #6
            Re: Download binary file (http)

            Fredrik,

            It should be pointed out that you are getting the WebRequest
            incorrectly. You should be using the following line:

            WebRequest objRequest = WebRequest.Crea te(url);

            The static Create method is used to create instances of the
            HttpWebRequest. What you are doing isn't wrong (as it works), but it isn't
            the recommended way of doing it.

            --
            - Nicholas Paldino [.NET/C# MVP]
            - mvp@spam.guard. caspershouse.co m

            "Fredrik" <eelector@hotma il.com> wrote in message
            news:e3787004.0 311200356.3c7f2 c8f@posting.goo gle.com...[color=blue]
            > How can I download a binary file (like .doc or .pdf) from http to my
            > harddrive using the .net framework?
            >
            > WebRequest objRequest = System.Net.Http WebRequest.Crea te(url);
            > objResponse = objRequest.GetR esponse();
            > BinaryReader bsRead = new BinaryReader(ob jResponse.GetRe sponseStream()
            > FileStream fs = new FileStream("tes t.doc", FileMode.Create New);
            > while(bsRead.Pe ekChar() != -1)
            > {
            > cbuf=bsRead.Rea dByte();
            > fs.WriteByte(cb uf);
            > }
            >
            > is one of the method I've tried. All of them failed becuase seeing in
            > the base stream wasn't supported. All tutorals and examples i've seen,
            > both on the internet and in msdn, deals with reading text, or a binary
            > file of predefined size.
            >
            > Downloading a .doc file using Webclient.Downl oadFile(...) resulted in
            > a file that seemed to have been treated like text, not binary.
            >
            > Please help![/color]


            Comment

            • Fredrik

              #7
              Re: Download binary file (http)

              Thank you all for the help!
              Now it works great!


              eelector@hotmai l.com (Fredrik) wrote in message news:<e3787004. 0311200356.3c7f 2c8f@posting.go ogle.com>...[color=blue]
              > How can I download a binary file (like .doc or .pdf) from http to my
              > harddrive using the .net framework?
              >
              > WebRequest objRequest = System.Net.Http WebRequest.Crea te(url);
              > objResponse = objRequest.GetR esponse();
              > BinaryReader bsRead = new BinaryReader(ob jResponse.GetRe sponseStream()
              > FileStream fs = new FileStream("tes t.doc", FileMode.Create New);
              > while(bsRead.Pe ekChar() != -1)
              > {
              > cbuf=bsRead.Rea dByte();
              > fs.WriteByte(cb uf);
              > }
              >
              > is one of the method I've tried. All of them failed becuase seeing in
              > the base stream wasn't supported. All tutorals and examples i've seen,
              > both on the internet and in msdn, deals with reading text, or a binary
              > file of predefined size.
              >
              > Downloading a .doc file using Webclient.Downl oadFile(...) resulted in
              > a file that seemed to have been treated like text, not binary.
              >
              > Please help![/color]

              Comment

              Working...