Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-17-2013, 06:35 AM   PM User | #1
ssen6547
New to the CF scene

 
Join Date: Jan 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
ssen6547 is an unknown quantity at this point
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
ssen6547 is offline   Reply With Quote
Old 01-17-2013, 06:57 AM   PM User | #2
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,335
Thanks: 13
Thanked 207 Times in 207 Posts
DanInMa is on a distinguished road
thats becuase activex only works in internet explorer
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Old 01-17-2013, 09:09 AM   PM User | #3
ssen6547
New to the CF scene

 
Join Date: Jan 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
ssen6547 is an unknown quantity at this point
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..
ssen6547 is offline   Reply With Quote
Old 01-17-2013, 02:09 PM   PM User | #4
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 374
Thanks: 3
Thanked 44 Times in 44 Posts
Airblader can only hope to improve
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
Airblader is offline   Reply With Quote
Old 01-17-2013, 09:59 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,549
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Old 01-18-2013, 08:09 AM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,100
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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
__________________

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.

Last edited by Philip M; 01-18-2013 at 08:15 AM..
Philip M is offline   Reply With Quote
Old 01-18-2013, 08:34 AM   PM User | #7
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 374
Thanks: 3
Thanked 44 Times in 44 Posts
Airblader can only hope to improve
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
Airblader is offline   Reply With Quote
Old 01-18-2013, 09:14 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,549
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Old 01-18-2013, 09:18 PM   PM User | #9
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 374
Thanks: 3
Thanked 44 Times in 44 Posts
Airblader can only hope to improve
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
Airblader is offline   Reply With Quote
Old 01-18-2013, 09:43 PM   PM User | #10
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,549
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
__________________
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.
Old Pedant is online now   Reply With Quote
Old 01-18-2013, 10:07 PM   PM User | #11
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 374
Thanks: 3
Thanked 44 Times in 44 Posts
Airblader can only hope to improve
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..
Airblader is offline   Reply With Quote
Old 01-18-2013, 11:02 PM   PM User | #12
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,549
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Old 01-19-2013, 04:33 AM   PM User | #13
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,553
Thanks: 9
Thanked 480 Times in 463 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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 5/13)
STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%
rnd me is offline   Reply With Quote
Old 01-19-2013, 08:56 AM   PM User | #14
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 374
Thanks: 3
Thanked 44 Times in 44 Posts
Airblader can only hope to improve
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.
Airblader is offline   Reply With Quote
Old 01-19-2013, 01:18 PM   PM User | #15
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 374
Thanks: 3
Thanked 44 Times in 44 Posts
Airblader can only hope to improve
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.
Airblader is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:33 PM.


Advertisement
Log in to turn off these ads.