开发者

How can I write a UTF-8-encoded file to the user’s computer using JavaScript on IE?

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 ) {
            wind开发者_StackOverflow社区ow.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.


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)


I have done something like this for a text editor. The exec command is proprietary (to IE). It allows the end-user to save the current web page via a dialog box. The meta tag has no effect in this. If you want the file to be saved as UTF-8, then create the file as UTF-8. Open Notepad, paste the HTML source and save the file as HTML but ensure you change the Encoding listbox to UTF-8. Now, open the page in IE and invoke the exec command, the dialog box would have preselected UTF-8.


Open notepad, paste the following code, and save the file with the Encoding listbox set to UTF-8. Next, open the file in Internet Explorer, click on the Information bar below the address box, choose to "Allow Blocked Content" below the toolbar, and click on the button. You will see that the Save HTML Document dialog box has the Language list set to Unicode (UTF-8). I am using IE8.

  <input type="button" 
         value="Save As UTF-8 file"
         onclick="document.execCommand('SaveAs',false,'somename.html');" />

If this is not what you want, then provide a full working sample file. Please remove unnecessary parts.


I think you are creating HTA?

For saving file as UTF8

Writing UTF8 text to file

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜