/    Sign up×
Community /Pin to ProfileBookmark

how to save a file using javascript

Hallo. I want to save a txt file using javascript. I have written some code, but it doesn’t work, I don’t know where is the problem. Please help me.

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml“>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Untitled Document</title>
</head>

<body>
<script language=”JavaScript” type=”text/javascript” >
function WriteFile()
{
var fh = fopen(“c:MyFile.txt”, 3); // Open the file for writing

if(fh!=-1) // If the file has been successfully opened
{
var str = “Some text here…”;
fwrite(fh, str); // Write the string to a file
fclose(fh); // Close the file
}

}
</script>
<input name=”button” type=”button” onMouseDown=”WriteFile()” value=”click to save a file” />
</body>
</html>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@J2NYCMay 30.2009 — Javascript works on the client side, not the server side. You can't use it to write to the server to my knowledge.

You need to use PHP or another server side tool.
Copy linkTweet thisAlerts:
@astupidnameMay 30.2009 — <script language="JavaScript" type="text/javascript" >

function WriteFile()

{

var fh = fopen("c:MyFile.txt", 3); // Open the file for writing

if(fh!=-1) // If the file has been successfully opened

{

var str = "Some text here...";

fwrite(fh, str); // Write the string to a file

fclose(fh); // Close the file

}

}

</script>[/quote]


fopen, fwrite, and fclose are php filesystem functions, not javascript functions. You would need to use ajax to communicate with a php script on the server, if you actually want to write files onto the server from javascript (don't know why you would). For info on ajax, see:

http://www.w3schools.com/php/php_ajax_intro.asp
×

Success!

Help @godzial__ spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...