Execcommand > What is happening? Anyone?

Discussion in 'Javascript' started by Sharon, Nov 15, 2004.

  1. Sharon

    Sharon Guest

    Hi!
    I need help. I have a variable which is just a string containing the
    output of a transform from XML to CSV format. I want the client to be
    able to download this as a file (.csv), so I used this piece of code:

    function SaveVarAsFile(someVar){
    alert(someVar);
    SaveFrame.document.open("text/plain","replace");
    SaveFrame.document.write(someVar);
    SaveFrame.document.close();
    SaveFrame.document.charset="UTF-8";
    SaveFrame.focus();
    SaveFrame.document.execCommand('SaveAs','false','test.csv');

    SaveFrame is an iframe in the body of my page:
    <iframe id="SaveFrame" style="display:none"></iframe>

    Now I don't want the encoding to be UTF-8 but I need it to be ASCII,
    in order to display the file properly in Excel instead of having all
    the data in one column. But when I change charset into "ASCII", the
    file that is saved is not the document in the iframe but the whole
    html page the iframe is part of! How is this possible and what can I
    do to fix it?
    Secondly, the saved file begins and ends with html tags that I don't
    want, I want it to be just plain text. How do I get rid of these tags?

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML><HEAD>
    <META http-equiv=Content-Type content="text/html;
    charset=utf-8"></HEAD>
    <BODY><PRE>

    Hope someone can help 'cause I'm getting really frustrated! Thanks.
     
    Sharon, Nov 15, 2004
    #1
    1. Advertisements

  2. Hello

    I don't know if this helps.

    I tested it for myself, and Microsoft seems to do something different
    depending on the extension you give the file name.

    If you use ".csv" then as you noted, it seems to save the HTML mark
    up.

    However if you use ".txt", then it will just save the plain text as
    desired.

    If you want to save with a file name ".csv" then you may need to use
    the ActiveX object "Scripting.FileSystemObject" but obviously this is
    only for browsers that support it, and it will depend on browser
    security settings.

    Julian
     
    Julian Turner, Nov 16, 2004
    #2
    1. Advertisements

  3. Sharon

    Sharon Guest

    Thanks, but I had already gotten rid of the HTML tags differently, by
    changing the MIME type of
    SaveFrame.document.open("text/plain","replace"); to text/html. What
    remains is the encoding problem, I get this neat file which looks like
    this:

    "id","datatype"
    "0","Unknown"
    "1","integer"
    "2","float"
    "3","string"
    "4","memo"
    "6","date-time"
    "7","boolean"

    but the encoding is unicode and Excel therefore does not separate the
    columns but shows the data above all in one column. When I open it
    with notepad, save it as ANSI and reopen it in Excel, it is displayed
    correctly in two columns without showing the commas and apostrophes.
    Who can help me? Thanks!
     
    Sharon, Nov 17, 2004
    #3
  4. Julian Turner, Nov 19, 2004
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.