0

I know that since the creation of HTML5 there has been many additions that make programming websites more easy than ever. It seems they may have failed to add a File system that actually creates a file in a specified directory (not to my knowledge anyway). I have been searching the web and have found nothing about doing such a thing without using a 3rd party i.e JQuery, php, Javascript Libraries. I assume that since there is pure js libraries that can accomplish this that it is possible, but I do not know for sure. What I would like to do is output an html file from the outerHtml generated in my JavaScript minus the script source itself. Note that I do not want the client to have to download.

1

2 Answers 2

1

Try this:

<head>    
<script type="text/javascript">
 function WriteToFile(passForm) {        
    set fso = CreateObject("Scripting.FileSystemObject");  
    set s = fso.CreateTextFile("C:\test.txt", True);
    s.writeline("HI");
    s.writeline("Bye");
    s.writeline("-----------------------------");
    s.Close();
 }
</script>   
</head>

<body>
<p>Fill out the form below:
</p>
<form onSubmit="WriteToFile(this)">
Type your first name:
<input type="text" name="FirstName" size="20">
<br>Type your last name:
<input type="text" name="LastName" size="20">
<br>
<input type="submit" value="submit">
</form> 

This will work only on IE.

Also see: create a file using javascript in chrome on client side

Sign up to request clarification or add additional context in comments.

2 Comments

is there any cross browser way of doing it?
Not that I'm aware of.
1

Preventing webpage scripts from creating files on local filesystem is done for your own security. You don't want website you visit create an executable file on your local disk with virus.

There is a File System API in HTML5, but it's kind of internal filesystem intended as a local storage for user data.

You can force user to save file by returning HTTP header Content-Disposition with value attachment; filename=example.csv. This will force browser to save file locally.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.