Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript code to create a text file

    <html>
    <h2>Create Text file in JavaScript</h2>
    <script>
    function createFile(){
    var object = new ActiveXObject("Scripting.FileSystemObject");
    var file = object.CreateTextFile("C:\\Hello.txt", false);
    file.WriteLine('Hello World');
    file.WriteLine('Hope is a thing with feathers, that perches on the soul.');
    file.Close();
    }
    </script>
    <input type="Button" value="Create File" onClick='createFile()'>
    </html>



    This code doesn't work, it throws error
    Uncaught ReferenceError: ActiveXObject is not defined
    When opened in google chrome version 24.0.1312.52 m

    Can anyone help on this.

    Moto of this code is to create a text file with some content using javascript

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    thats becuase activex only works in internet explorer

  • #3
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Tried but didnt work

    Quote Originally Posted by DanInMa View Post
    thats becuase activex only works in internet explorer

    Thanks for the reply.

    I tried with internet explorer, but it throws error as below.


    Webpage error details

    User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.3; .NET4.0C; .NET4.0E)
    Timestamp: Thu, 17 Jan 2013 07:30:08 UTC


    Message: Unterminated string constant
    Line: 5
    Char: 65
    Code: 0
    URI: file:///C:/Data/Downloads/writeTo%20textFfile/writeTo%20textFfile.html


    Message: Object expected
    Line: 12
    Char: 1
    Code: 0
    URI: file:///C:/Data/Downloads/writeTo%20textFfile/writeTo%20textFfile.html


    Message: Object expected
    Line: 12
    Char: 1
    Code: 0
    URI: file:///C:/Data/Downloads/writeTo%20textFfile/writeTo%20textFfile.html


    When i try with Mozilla firefox with that add on firebug. It throws error as
    ReferenceError: createFile is not defined

    Could you please help me to modify the code so that it works, if the code is having problem.
    My intention is create a webpage with some form and controls, once the user open the webpage (with the use of webpage file anywhere) and fills the data in the form and click submit button, a notepad file should be saved in his local drive under defined path. No client/server communication.
    Last edited by ssen6547; 01-17-2013 at 09:12 AM.

  • #4
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    JavaScript cannot safe files on the client's computer. A nice tool to use is Downloadify ( https://github.com/dcneiner/Downloadify ). It's a little Flash application that you can easily style as a button. Using JavaScript you can feed it with a text and then by clicking on it it will allow the user to save the file.

    air

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,171
    Thanks
    75
    Thanked 4,339 Times in 4,305 Posts
    Airblader is very very wrong.

    MSIE *CAN* save files on the client computer.

    Providing that the user allows it to by setting the security settings.

    There is nothing wrong with the JS code as shown in that first post.

    The error about "unterminated string constant" is simply not true.

    So ssen must have changed the code before trying it with MSIE.

    Now, having said all that...

    The code will almost surely not run.

    First of all, unless SSEN has changed the security settings in his MSIE browser, he won't be allowed to create the FileSystemObject.

    Secondly, it is very unlikely that the browser will have permission to create a file in the C:\ directory. SSEN should be using some sub-directory that is used for *NOTHING* except junk file like this and that is marked as WRITABLE by the browser.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    I have to say that it worked for me in IE9, with a warning message.

    See also HELP: Copy and Save to a notepad onclick
    Last edited by Philip M; 01-18-2013 at 08:15 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Quote Originally Posted by Old Pedant View Post
    MSIE *CAN* save files on the client computer.
    Yes, MSIE can – but who cares about MSIE only? Typically you wanna go for browser compatibility, don't you? Calling my statement "very wrong" is in fact very wrong as it is true in most (and more importantly, most relevant) cases.

    air

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,171
    Thanks
    75
    Thanked 4,339 Times in 4,305 Posts
    Okay, then, Airblader. Let's just say your answer is irrelevant.

    Of course you are right in the general case of all browsers. But...

    Because *CLEARLY* this line in his code:
    Code:
        var object = new ActiveXObject("Scripting.FileSystemObject");
    limits the use of his code to MSIE only.

    So giving an answer that is not MSIE-only is irrelevant to his code.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    I took it as him just copying something he found somewhere online without knowing that it would only work in MSIE. I know very few examples when something was developed for MSIE and MSIE only – all of which were professional enterprise solutions*.
    I think it's not safe to infer from one line of code of a beginner that he wanted to go for a MSIE only solution. Even more so because he was using FireFox. Why would he wonder that it doesn't work in FF if he knew it's MSIE only?

    *) Although you could argue that MSIE and "professional" are opposites.

    air

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,171
    Thanks
    75
    Thanked 4,339 Times in 4,305 Posts
    OMG! You are *SO RIGHT* Airblader!

    I am an utter DOOFUS! Or at the very least I am blind! (Almost true, this week...one of my eyes has triple vision temporarily.)

    I *UTTERLY MISSED*:
    Uncaught ReferenceError: ActiveXObject is not defined
    When opened in google chrome version 24.0.1312.52 m
    Forget *EVERYTHING* I have said in this thread! Clearly it's all irrelevant.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    You certainly are being childish ... rendering this discussion useless. The last time I checked Google Chrome wasn't the same as MSIE, so my point is still valid. Besides, it doesn't even matter – he never specifically asked for MSIE-only compatibility and I didn't see enough reason to infer it from his posting, so I provided a cross-browser solution.
    Last edited by Airblader; 01-18-2013 at 10:12 PM.

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,171
    Thanks
    75
    Thanked 4,339 Times in 4,305 Posts
    What? I am admitting that I completely blew my answer. It was *MY* answer that was completely irrelevant, not yours. Why does that bother you?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,296
    Thanks
    10
    Thanked 583 Times in 564 Posts
    this works great in chrome, full FF support should kick in starting early spring:

    Code:
    function download(strData, strFileName, strMimeType){
      var  D=document, A=arguments, a=D.createElement("a"), 
           d=A[0], n=A[1], t=A[2]||"text/plain";
    
        //build download link:
           a.href="data:" +strMimeType+ "," +escape(strData);
    
    if('download' in a){
         a.setAttribute("download", n);
         a.innerHTML="downloading...";
         D.body.appendChild(a);
        setTimeout(function(){
    	var e= D.createEvent("MouseEvents");
    	e.initMouseEvent(
    		"click", true, false, window, 0, 0, 0, 0, 0
    		, false, false, false, false, 0, null
    	);
           a.dispatchEvent(e);
           D.body.removeChild(a);
        }, 66 );
      return true;
    };//end if a[download]?
    
     //do iframe dataURL download:
        var f=D.createElement("iframe");
           D.body.appendChild(f);
            f.src="data:" +(A[2]?A[2]:"application/octet-stream")+ (window.btoa?";base64":"") +","  +(window.btoa?window.btoa:escape)(strData);
            setTimeout(function(){D.body.removeChild(f);}, 333);
        return true;
    }//end download()
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #14
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Quote Originally Posted by Old Pedant View Post
    What? I am admitting that I completely blew my answer. It was *MY* answer that was completely irrelevant, not yours. Why does that bother you?
    I thought you were being sarcastic. For that misunderstanding i apologize.

  • #15
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Why do you insist on JavaScript? I gave you an alternative that is much more stable. For pure JavaScript to be able to do this cross-browser and reliably you will have to wait – indefinitely.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •