473,651 Members | 3,007 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help With Using System.Text.Enc oding 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.Sock et. 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.Enc oding function?

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

while(ret != 0)
{

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

string sResponse =
System.Text.Enc oding.GetEncodi ng(1252).GetStr ing(buffer);

bw.Write(sRespo nse);

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

bw.Close();

Sep 15 '06 #1
4 2033
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.co m

<A_*********@ho tmail.comwrote in message
news:11******** **************@ p79g2000cwp.goo glegroups.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.Sock et. 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.Enc oding function?

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

while(ret != 0)
{

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

string sResponse =
System.Text.Enc oding.GetEncodi ng(1252).GetStr ing(buffer);

bw.Write(sRespo nse);

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

bw.Close();

Sep 15 '06 #2
<A_*********@ho tmail.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.co m>
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_*********@ho tmail.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.co m>
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(buffe r);
FileStream fs = new FileStream(@"C: \sample.doc", FileMode.Append ,
FileAccess.Writ e);
BinaryWriter bw = new BinaryWriter(fs );

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

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

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

Sep 15 '06 #4
<A_*********@ho tmail.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.co m>
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
2175
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 am able to check using xpath in asp.net 2.0 till MenuItem node. if i check visible attribute value till SubMenuLevel0 node then in tree it will not display the MenuItem Node at all Note: My tree Menu will start from MenuItem node and it will...
8
5462
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 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I debug it in PHP designer, it works with no problems, I get the test email. If
3
1779
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 have have successfully sent and recieved using the PING, AUTHENTICATE (send in username and password and return a secuirity token), SOLICIT(send in information for a query and get a number indicating the query),
34
4831
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? (code should work smoothly across different machine.) Thanks, Ann
0
3691
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 rare lacquered tea caddy c1840. The outside of the caddy has wonderful scenes on each side which are really finely painted. The caddy stands on claw feet. Inside there are four tin canister which is very rare to see on such a small caddy. All canisters...
6
3346
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 the past in other places, and while the help was much appreciated, it seemed everyone just wanted to 'theoretically' explain how to do it, but when I tried to do it myself, I couldn't login. I want to simply pass the email address and password to...
0
5557
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 ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
2
1233
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 software. using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts;
3
8270
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 build with these methods, will appear to encrypt and decrypt, but the resulting decrypted file will be corrupted. I tried encrypting a .bmp file and then decrypting, the resulting decrypted file under .NET 2.0 is garbage, the .NET 1.1 build works...
0
8278
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
8584
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
7299
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...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5615
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
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1912
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1588
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.