I am writing a local file using JavaScript and I am using IE for the same.
My code is as follows:
function savefile( f,g){
var w = window.frames.w;
if( !w ) {
w = document.createElement( 'iframe' );
w.id = 'w';
w.style.display = 'none';
document.body.insertBefore( w );
w = window.frames.w;
if( !w ) {
w = window.open( '', '_temp', 'width=100,height=100' );
if( !w ) {
window.alert( 'Sorry, could not create file.' );
return false;
}
}
}
var d = w.document;
d.open( 'text/xml', 'replace');
d.charset = "UTF-8";
d.write(JWPFormToHTML(f));
d.close();
var name= g.filename.value;
if( d.execCommand( 'SaveAs', false , name ) )
{
g.filename.value=name;
//document.getElementById("filename").value="";
alert('File has been saved.' );
}
else
{
alert( 'The file has not been saved.\nIs there a problem?' );
}
w.close();
return false;
}
The problem I am facing is the file is not getting saved as a UTF-8 encoded file, although I have added <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
to it.
Please help me on this or suggest some alternative to me.
NOTE: I would like a file manager to open before the file gets saved as in the case of execCommand
.
js is a well-tested UTF-8 encoder/decoder written in JavaScript. Unlike many other JavaScript solutions, it is designed to be a proper UTF-8 encoder/decoder: it can encode/decode any scalar Unicode code point values, as per the Encoding Standard.
Most JavaScript engines use UTF-16 encoding, so let's detail into UTF-16. UTF-16 (the long name: 16-bit Unicode Transformation Format) is a variable-length encoding: Code points from BMP are encoded using a single code unit of 16-bit. Code points from astral planes are encoded using two code units of 16-bit each.
Most likely you have an issue with the fact that Windows typically encodes unicode at utf-16 and the browser doesn't care to consider any alternative.
You could go the ActiveX route, The FileSystemObject supports a unicode format flag for text streams but I'd wager it's the same 16 encoding. The ADODB.Stream object however contains a charset property which can be set to a variety of formats including utf-8
http://msdn.microsoft.com/en-us/library/ms526296(v=exchg.10).aspx
Outside of that I think your best bet would be to write a bho or have the specs changed. You will of course need higher permissions manually changed in the browser, but maybe you're lucky and this is an intranet application :D
var adTypeBinary = 1;
var adTypeText = 2;
var adSaveCreateOverwrite = 2;
var stream = new ActiveXObject("ADODB.Stream");
stream.Type = adTypeText;
stream.Charset = "utf-8";
stream.Open();
stream.Write(txt);
stream.SaveToFile(path, adSaveCreateOverwrite);
(*this code was not tested, for example only)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With