CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   Javascript code to create a text file (http://www.codingforums.com/showthread.php?t=285922)

ssen6547 01-17-2013 06:35 AM

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

DanInMa 01-17-2013 06:57 AM

thats becuase activex only works in internet explorer

ssen6547 01-17-2013 09:09 AM

Tried but didnt work
 
Quote:

Originally Posted by DanInMa (Post 1306811)
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.

Airblader 01-17-2013 02:09 PM

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

Old Pedant 01-17-2013 09:59 PM

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.

Philip M 01-18-2013 08:09 AM

I have to say that it worked for me in IE9, with a warning message.

See also http://www.codingforums.com/showthread.php?t=262759

Airblader 01-18-2013 08:34 AM

Quote:

Originally Posted by Old Pedant (Post 1306975)
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

Old Pedant 01-18-2013 09:14 PM

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.

Airblader 01-18-2013 09:18 PM

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

Old Pedant 01-18-2013 09:43 PM

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*:
Quote:

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.

Airblader 01-18-2013 10:07 PM

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.

Old Pedant 01-18-2013 11:02 PM

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?

rnd me 01-19-2013 04:33 AM

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()


Airblader 01-19-2013 08:56 AM

Quote:

Originally Posted by Old Pedant (Post 1307252)
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.

Airblader 01-19-2013 01:18 PM

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.


All times are GMT +1. The time now is 05:47 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.