473,509 Members | 2,918 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String encoding Converting and Save File Problem in IE

gnv
Hi all,
I am writing a cross-browser(i.e. 6 and netscape 7.1) javascript
program to save an XML file to local file system.

I have an xml string like below:

var xmlStr = "<?xml version="1.0" encoding="UTF-8"?><a>some info</a>";

I want to save this xml file to local file system with JavaScript,

in Netscape 7.1, this is easy with XPCOM:

var uConv = Components.classes['@mozilla.org/intl/scriptableunicodeconverter'].createInstance(Components.interfaces.nsIScriptabl eUnicodeConverter);
//uConv = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Components.interfaces.nsIUTF8Conve rterService);

uConv.charset = "UTF-8";

var xmlStrUTF8 = uConv.ConvertFromUnicode(fileStr);

then save the utf-8 encoded file to local file system.

But how can I do the same stuff in IE 6.0?

I tried two approaches but not working:
1.
convert the xml string to XML DOM object, try to use the XML DOM save
method, but IE6.0 use msxml 3.0 and it doesn't work by calling save()
from browser due to security issue.

2.
using ActiveX filesystem object's createTextFile method, then call
write() method. But the problem is the write() method only can save a
text file with ASCII encoding or utf-16 encoding. My xml file needs
"utf-8" encoding.

So I am wondering how to do it in IE. or if there is some small free
window utility I can call from Javascript to convert a utf-16 to
utf-8?

Thanks much,

v.
Jul 23 '05 #1
2 13076


gnv wrote:

I am writing a cross-browser(i.e. 6 and netscape 7.1) javascript
program to save an XML file to local file system.

I have an xml string like below:

var xmlStr = "<?xml version="1.0" encoding="UTF-8"?><a>some info</a>";

I want to save this xml file to local file system with JavaScript,
But how can I do the same stuff in IE 6.0?

I tried two approaches but not working:
1.
convert the xml string to XML DOM object, try to use the XML DOM save
method, but IE6.0 use msxml 3.0 and it doesn't work by calling save()
from browser due to security issue.
As all your stuff is local it seems you could use a HTML application
(.hat file) instead of a .html file, there you should be able to call
save() successfully. Of course .hta is not cross browser.
2.
using ActiveX filesystem object's createTextFile method, then call
write() method. But the problem is the write() method only can save a
text file with ASCII encoding or utf-16 encoding. My xml file needs
"utf-8" encoding.


Any XML parser should be able to cope with both UTF-8 and UTF-16 so I am
not sure why you need UTF-8. I don't see a way to get UTF-8 with the
FileSystemObject unless you know you have only characters in the range
0..127 as there (at least on a Western European Windows) the local
codepage should yield the same encoding as UTF-8.
Some people use ADODB.Stream to read/write binary files with script on
Windows, the docs are at
http://msdn.microsoft.com/library/de...dobjstream.asp
you might be able to use ADODB.Stream for your purpose. I see you can
even set the Type to text, set a Charset and then use WriteText and
SaveToFile. Thus a quick test with a Windows Script Host JScript

var stream = new ActiveXObject('ADODB.Stream');
stream.Type = 2; // 2 is type text (adTypeText)
stream.Charset = 'UTF-8';
var xml = '<?xml version="1.0" encoding="UTF-8"><root>You have to pay
2.50 € if you use German umlauts ä, ö, ü.<\/root>';
stream.Open();
stream.WriteText(xml);
stream.SaveToFile("test20040602.xml", 2);
stream.Close();

seems to yield an UTF-8 encoded XML file. However it has a byte order
mark it seems at the beginning which some XML parsers don't like.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 23 '05 #2
gnv
Thank you so much for your help, Martin. I will use ADODB.Stream, then
javascript commandline program to remove the byte order mark as
workaround since I will change my current codebase alot if I use hta.

the reason using UTF-8 instead of UTF-16 is that the majority content
of the XML document is ASCII, so UTF-16 double the file size.

Thanks again!!

v.
Jul 23 '05 #3

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

Similar topics

5
3201
by: Magnus | last post by:
Hi, I'm programatically posting an image using multipart/form-data. It sends to the server OK, but when I try to view it in the browser, it is still in the base64 string I sent it as:...
8
3655
by: Marius Cabas | last post by:
Hi, I'm a beginner so don't shoot ;) I'm reading a wave file into a byte and I'm trying to convert the result to String but the converted string is altered, so if I'm generating a new wave file...
4
3154
by: Andrew Inwards | last post by:
Is it possible to save a MS Word document in a string? I suspect not because Ive tried saving it in using various encoding formats and it always gets corrupted. Thanks Andrew
5
4819
by: AC [MVP MCMS] | last post by:
Any pointers on how to (1) read a Base64 encoded string from a text file and (2) write it to a binary file? I have a ton of files that are being generated from a legacy system. Each file...
2
13099
by: Edvin | last post by:
Why is binary array written to a file different than when converting the binary array to string and then writing it to file! For example: // --- Allocate byte array byte arrByte = new byte;...
1
1559
by: vinki | last post by:
Hi Everyone, I am constructing an xml file, but when I try to read it there are some extra slashes in the output. Can anoyone tell me why there are some extra slashed and can i send the file to...
4
8213
by: J Peyret | last post by:
Well, as usual I am confused by unicode encoding errors. I have a string with problematic characters in it which I'd like to put into a postgresql table. That results in a postgresql error so I...
5
3392
by: andrew.douglas11 | last post by:
Hello, I'm looking to display an image in the browser using a binary string containing all the bytes that make up a GIF image. I've tried all the standard encodings in System.Text.Encoding,...
2
5563
by: SammyBar | last post by:
Hi all, I'm trying to convert the xml obtained from a XmlReader object into a UTF-8 array. My general idea is to read the XmlReader and write into a MemoryStream. Then convert the MemoryStream...
0
7237
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,...
0
7137
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...
0
7416
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...
0
7506
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...
0
5656
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,...
1
5062
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...
0
4732
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...
0
1571
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 ...
1
779
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.