Help With Using System.Text.Encoding To Download File

A

A_StClaire_

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(buffer);

bw.Write(sResponse);

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

bw.Close();
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
J

Jon Skeet [C# MVP]

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.
 
A

A_StClaire_

Jon said:
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.


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();
 
J

Jon Skeet [C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top