10

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.

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
Varun
  • 5,001
  • 12
  • 54
  • 85
  • 1
    IE lets you save to the local machine via JavaScript? :P – alex Dec 13 '10 at 06:10
  • 5
    @alex: IE + ActiveX: lots of (nonportable) trickery ;-) – darioo Dec 13 '10 at 06:14
  • it does allow hell lot of things using Active X... that are so complicated in itself and are not supported on other browsers :(... makes life of developers horrible :P – Varun Dec 13 '10 at 06:19
  • 2
    IMO web page should never write to file on the local machine. It's like you invite a guest to your house and he starts to re-decorate it. So the problem start with the non standard and bad requirement to begin with. – Shadow The GPT Wizard Dec 13 '10 at 13:10
  • Its the functional requirement of our project and has to be done... it wont be done from a server.... there will be a local html file that will generate another htm file.. – Varun Dec 13 '10 at 15:25
  • i am still not able to do this... but managed to convert UTF-16 file to be read as UTF-8 please follow the [link](http://stackoverflow.com/questions/4427847/i-have-a-htm-file-can-i-read-it-as-utf-8-formatted-file-without-doing-anything-to/4438124#4438124) – Varun Dec 14 '10 at 10:28
  • 1
    I feel your pain, varun. I suppose it's too late to tell the client that this solution is gonna be hell to maintain and also open a huge security hole? I strongly urge anyone who faces these kinds of demands/requirements to return to the client with a big "why?" and explain the risks, early in the process. With all due respect to you who are called upon when it's too late for that. – Niklas Wulff Dec 20 '10 at 17:37
  • @Niklas - thanks for understanding the pain.. but the thing is there should be some way of doing this. ifound this 1 (code above) but it writes in UTF-16 format and not in UTF-8 so need something that can write in UTF-8 – Varun Dec 20 '10 at 18:15
  • 4
    @varun - your client is binding themselves to a particular browser for the lifetime of the application doing this. Most likely they'll forget, get an upgrade, break the app and then find they don't have support or the ability to continue business process. This would be better achieved with some kind of plugin or simple server-side storage and download. Sometimes it's ok to say no. – annakata Dec 21 '10 at 09:10

4 Answers4

4

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)

Marcus Pope
  • 2,293
  • 20
  • 25
0

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.

BZ1
  • 1,306
  • 2
  • 8
  • 10
  • If you provide a sample HTML source that demonstrates the problem in IE, maybe it can be solved. – BZ1 Jan 06 '11 at 05:15
  • It doesnt have preselected UTF-8 infact doesnt have any encoding mentioned over there – Varun Jan 11 '11 at 05:02
0

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.

BZ1
  • 1,306
  • 2
  • 8
  • 10
  • tried it and it works fine...but the code snippet i have given doesnt saves the normal page it takes the output from the page and generates a dummy page and tries to save it , so is there a way to do the same please refer the complete code provided above – Varun Jan 13 '11 at 08:14
  • Post the code complete with HTML tags. Otherwise it is difficult to make out which elements you are referring. – BZ1 Jan 13 '11 at 12:07
-1

I think you are creating HTA?

For saving file as UTF8

Writing UTF8 text to file

Community
  • 1
  • 1
OnesimusUnbound
  • 2,886
  • 3
  • 30
  • 40
  • both links suggested didnt helped me.. the second seems usefull but it needs a path and will not show save as dialog box :( which is main requirement of the code – Varun Dec 21 '10 at 12:11