473,385 Members | 1,356 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,385 software developers and data experts.

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 13064


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
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
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
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
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
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
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
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
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.