2

So I have a web page, and I would like to programaticly create a text file (lets say it has the words 'hello, I am a text file' in it) on a new directory on my website. The program will be in another directory on the website.

e.g. https://www.example.com/txtbuild.html is trying to programaticly make https://www.example.com/texts/hi.txt

Is there a way to do this with HTML/Javascript?

EDIT: I am on Github

3
  • 1
    No, it is impossible merely by HTML/JS since it is server-side. Commented Mar 30, 2017 at 3:00
  • If you are talking about client-side file create, check this link: stackoverflow.com/questions/5403912/… Commented Mar 30, 2017 at 3:03
  • Why do you want that? Commented Mar 30, 2017 at 5:45

5 Answers 5

3

You can't do it with HTML/Javascript alone, you need a functional language on the backend (nodejs, php, python)

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

3 Comments

So if using NodeJS wouldn't that be doing it with HTML/Javascript alone? (Just not all client-side JS.)
You'd be doing it in Javascript alone. But that's only because Node uses JS. If you did it in ASP.Net it'd be C# alone, or in Django it'd be Python alone. Whatever web framework you choose to use is going to be what creates the files on the server for you.
@nnnnnn "I have a web page" this was in the question, so I assumed he means client-side javascript
0

You can use ActiveXObject, but it won't work in all browsers.

var fso = new ActiveXObject("Scripting.FileSystemObject");
var a = fso.CreateTextFile("c:\\testfile.txt", true);
a.WriteLine("This is a test.");
a.Close();

https://msdn.microsoft.com/en-us/library/5t9b5c0c(v=vs.84).aspx

2 Comments

Note in the question it says the new file should be accessible on the web server.
FYI (a) ActiveX is a thing of the past, and not available in Edge – it was never a good idea, and never worked on other browsers or operating systems; (b) ActiveX works only on the client, if at all, and, thankfully, doesn’t save on the web site, which I think is mentioned somewhere in the question.
0

If, when you say "JavaScript", you're referring to a node.js application running on a server, then this is possible. It doesn't have to be node though; it could be a Django site, or an ASP.Net site, doesn't matter. You can't have JS code in the browser create files on your server... the JS in the browser is executing on a client machine, and doesn't have access to the server's file system.

You could create an endpoint to which your clients could send requests that would initiate the creation of the file.

You could also allow clients to PUT or POST files to your server, but again, this is something you control from the server side of the application. Your webpage (i.e., HTML file as you put it) cannot create files on the server itself. Your server allows clients to send it files in a specific manner, and the client must adhere to those rules.

Comments

0

The short answer to your question is no.

The long answer is that you have the following alternatives:

  • Use a form on the Browser end, send the data back to the server, and then use a server-side language such as PHP to receive and save the data. No JavaScript required, but you do need server-side programming.
  • You can make the process more immediate by using JavaScript on the browser end to send data back to the server. This is called Ajax. You will still need server side processing, though.

Note that it is probably a very bad idea to simple accept user data and save it directly. There are two things you should consider in your development:

  • Always filter the incoming data against the possibility of receiving and accepting carefully crafted malicious data.
  • Consider storing the data in a database. Apart from being easier to manage (you don’t have to worry about filenames, for example), they can do less damage there.

Comments

0

You can achieve this in IE browser using the following code.

<SCRIPT LANGUAGE="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();

}

if you are looking for a goos reliable solution then better to use PHP and other server scripts.

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.