473,386 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Help With Using System.Text.Encoding To Download File

hi,

I am using the following code to download multiple file types from a
server. .txt files transfer fine. however Word .doc files come
through garbled and I don't know enough about encoding to understand
why.

the object 's' is a Mentalis.org component that, for all purposes here,
is a System.Net.Socket. the line I commented out is one that
hard-codes the encoding to 'ASCII' which I assume would only hurt me.

I know I am sending the request correctly. can anyone see how or if I
am misusing the System.Text.Encoding function?

thx, all
byte[] buffer = new byte[4096];
int ret = s.Receive(buffer);
FileStream fs = new FileStream(@"C:\sample.doc", FileMode.Append,
FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);

while(ret != 0)
{

// string sResponse = Encoding.ASCII.GetString(buffer, 0, ret);

string sResponse =
System.Text.Encoding.GetEncoding(1252).GetString(b uffer);

bw.Write(sResponse);

if(s.Available != 0)
ret = s.Receive(buffer);
else
ret = 0;
}

bw.Close();

Sep 15 '06 #1
4 2021
This won't work. .doc files are word documents, and while they have
text in them, they are in a format that is not a matter of encoding, it's a
completely separate file format.

If you want to read the text from .doc files, you need to open it
through Word (through automation) and then access the document through the
object model.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<A_*********@hotmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
hi,

I am using the following code to download multiple file types from a
server. .txt files transfer fine. however Word .doc files come
through garbled and I don't know enough about encoding to understand
why.

the object 's' is a Mentalis.org component that, for all purposes here,
is a System.Net.Socket. the line I commented out is one that
hard-codes the encoding to 'ASCII' which I assume would only hurt me.

I know I am sending the request correctly. can anyone see how or if I
am misusing the System.Text.Encoding function?

thx, all
byte[] buffer = new byte[4096];
int ret = s.Receive(buffer);
FileStream fs = new FileStream(@"C:\sample.doc", FileMode.Append,
FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);

while(ret != 0)
{

// string sResponse = Encoding.ASCII.GetString(buffer, 0, ret);

string sResponse =
System.Text.Encoding.GetEncoding(1252).GetString(b uffer);

bw.Write(sResponse);

if(s.Available != 0)
ret = s.Receive(buffer);
else
ret = 0;
}

bw.Close();

Sep 15 '06 #2
<A_*********@hotmail.comwrote:
I am using the following code to download multiple file types from a
server. .txt files transfer fine. however Word .doc files come
through garbled and I don't know enough about encoding to understand
why.
You shouldn't be useing an encoding at all. Word files are binary files
- Encodings are only to be used for plain text files. You should just
use Streams to read/write binary files.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 15 '06 #3
Jon wrote:
<A_*********@hotmail.comwrote:
I am using the following code to download multiple file types from a
server. .txt files transfer fine. however Word .doc files come
through garbled and I don't know enough about encoding to understand
why.

You shouldn't be useing an encoding at all. Word files are binary files
- Encodings are only to be used for plain text files. You should just
use Streams to read/write binary files.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

thx guys but the following code produces the same results even though
encoding is not involved. I am, or think I am, writing bytes directly
to file.

apart from being garbled, the file I receive is 8K. the one that was
sent is 19K. anyone know what's happening?
byte[] buffer = new byte[4096];
int ret = s.Receive(buffer);
FileStream fs = new FileStream(@"C:\sample.doc", FileMode.Append,
FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);

while(ret != 0)
{
bw.Write(buffer);

if(s.Available != 0)
ret = s.Receive(buffer);
else
ret = 0;
}

fs.Close();
bw.Close();

Sep 15 '06 #4
<A_*********@hotmail.comwrote:
thx guys but the following code produces the same results even though
encoding is not involved. I am, or think I am, writing bytes directly
to file.

apart from being garbled, the file I receive is 8K. the one that was
sent is 19K. anyone know what's happening?
1) Don't use Available - that says whether any more data is available
*now*, not whether there's any more left to come. You'll need a
different way of indicating when the transfer is finished (eg the other
end closing the socket, or providing a length before the transfer
starts)

2) You're currently writing the *whole* buffer each time, even if you
haven't read a whole buffer. You should only write as much as you've
received.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 16 '06 #5

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

Similar topics

0
by: Tree menu using XML | last post by:
I have one XML file that has nodes and sub node and each and every node has the attribute call visible if its value is true then diplay this node else don't display thid node, but this condition i...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
3
by: Rodney Garland | last post by:
Hi All, I am a relative beginner to Python and am looking for help with sending and XML message and getting back a return file. The server is: https://node.deq.state.or.us/node/node.asmx I...
34
by: Ann | last post by:
I am opening a file which looks like 0xABCDEF01 on another machine but 0x01EFCDAB on my machine. Is this a byte swapping? Could anyone give a good way to check if bytes are being swapped?...
0
by: samjam | last post by:
Below is some coding in a program i am using, i would like to know how i can get the text bigger or bolder on my webpage, This is the section of text i would like bigger or bolder (This is a very...
6
by: AppleBag | last post by:
I'm having the worst time trying to login to myspace through code. Can someone tell me how to do this? Please try it yourself before replying, only because I have asked this a couple of times in...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
2
by: mercenary | last post by:
i have a textbox in which i write in bangla(Bengali). but when i try to convert the bangla text into a GIF file the image shows ????? or something like a box. By the way i used AVRO Bangla typing...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.