473,769 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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!
Nov 15 '05 #1
6 32173
Fredrik <ee******@hotma il.com> wrote:
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.


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 - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
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
-----Original Message-----
How can I download a binary file (like .doc or .pdf) from http to myharddrive using the .net framework?

WebRequest objRequest = System.Net.Http WebRequest.Crea te (url);objResponse = objRequest.GetR esponse();
BinaryReader bsRead = new BinaryReader (objResponse.Ge tResponseStream ()FileStream fs = new FileStream("tes t.doc", FileMode.Create New);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 becuase seeing inthe 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 binaryfile of predefined size.

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

Nov 15 '05 #3
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
Nov 15 '05 #4

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

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #5
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]
- mv*@spam.guard. caspershouse.co m

"Fredrik" <ee******@hotma il.com> wrote in message
news:e3******** *************** ***@posting.goo gle.com...
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!

Nov 15 '05 #6
Thank you all for the help!
Now it works great!
ee******@hotmai l.com (Fredrik) wrote in message news:<e3******* *************** ****@posting.go ogle.com>...
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!

Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
3780
by: Kevin Knorpp | last post by:
Hello. I need to be able to extract the data from the attached file (or any file in the same format) so that I can work with the data in PHP. I'm fairly comfortable with using PHP with databases, arrays, etc. but have not worked with binary data files. http://www.performancecentral.net/C...3Loop/3Loop.php click on "Download Performance".
5
1745
by: travelling_nerd | last post by:
Hi, I'm trying to write a script that will allow validated users to download a file that has specific ntfs permissions. Here's a summary: Scenario: 1) The name of the file is "binary.zip". 2) I've created a local account on the server called "dl_user". 3) dl_user is the only account that has permissions on binary.zip. 4) I've written a script that validates several people to access a web
3
1894
by: Hanover | last post by:
I have looked fot his solution, but Im not sure what to search for. I have an asp.net based report. I need the user to be able to download an XML file of the results so they can use the data in Excel, etc. I do NOT want to write the file to the server first. I just want to be able to write the contents of the Dataset into a binary file and send it back out to the user as a download, but all in memory. Can you give me a boost in the...
2
1225
by: James Cooke | last post by:
I succesfull yam able to download binary data by calling a page called BinaryData.aspx. BinaryData.aspx gets the binary data from the database, and displays it as such: Response.Buffer = True Response.ClearContent() Response.ClearHeaders() Response.ContentType = binaryFieldType Response.BinaryWrite(binaryField) Response.End()
3
2573
by: Loane Sharp | last post by:
Hi there I'm new to VB.NET programming and really programming in general. I have a small binary file on my website that I want to download programmatically onto my local hard drive. I can locate the file on my website using (System.Net. namespace) ... Dim uri As New Uri(<web address goes here>)
1
1895
by: suresh_nsnguys | last post by:
Hi to all, I am new to this forum,I am new to this mobile technology.Right now i am creating one website using XHTML MP and PHP. I don't know how to download a binary file to mobile from PHP.Is it possible to download a file to mobile similar to desktop(PC) - like displaying 'save as ' dialog box. I want to know commonly supported file format for all types of mobiles.Bcz i tried out by downloading...
27
5123
by: Jeff | last post by:
Im trying to figure out why I cant read back a binary file correctly. I have the following union: #define BITE_RECORD_LEN 12 typedef union { unsigned char byte; struct { unsigned char type; /* 0 Type */ unsigned char sn; /* 1-3 Serial Number */
22
11782
by: xiao | last post by:
Can I fullfill this task? Using fred and fwrite?
11
10747
by: momatree48 | last post by:
tried to download parental safety software but i cannot download binary files. This is the screen that I get. ParentalControlSetup.exe which is a binary file from: http://dnl.crawlerparentalcontrol.com would you like to save this file? When I click save, in my downloads it says canceled. If I double click it it says< "Not a valid win32 application". I don't understand this.
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9416
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9850
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8862
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3948
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.