Javascript - create text file on website

前端 未结 5 661
庸人自扰
庸人自扰 2021-01-29 07:39

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 pr

相关标签:
5条回答
  • 2021-01-29 07:46

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

    0 讨论(0)
  • 2021-01-29 07:50

    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.
    0 讨论(0)
  • 2021-01-29 07:58

    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

    0 讨论(0)
  • 2021-01-29 08:01

    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.

    0 讨论(0)
  • 2021-01-29 08:05

    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.

    0 讨论(0)
提交回复
热议问题