-1

I need to write to the file system of my web application. Please don't respond "you can't do that" because it isn't helpful. I see that it can be done with HTML5, php, python, etc. I am just doing 1 basic write. The written file is crawled by a search engine, which is why I have this weird requirement.

To perform the file read I do something like this:

$.ajax({
    type: "POST",
    url: "./engine-text.txt",
    dataType: "text",
    async: false,
    success: function (data) {
       text_data = data;
    }
});

I'd like to do something simple to write. I'm just adding or removing lines of text to this file and writing it back. As it stands I don't have php or python. I basically have JQuery, HTML5, and Ruby. Remember another program needs to be able to read this file, so I don't think HTML5 will work. Thanks!

Joshua Hedges
  • 307
  • 4
  • 16
  • This library is super simple to use: https://github.com/eligrey/FileSaver.js/ – ControlAltDel Jul 17 '18 at 14:27
  • 2
    Possible duplicate of [Is it possible to write data to file using only JavaScript?](https://stackoverflow.com/questions/21012580/is-it-possible-to-write-data-to-file-using-only-javascript) – Sujit Agarwal Jul 17 '18 at 14:28
  • You need to write server-side code using AJAX. – SLaks Jul 17 '18 at 14:29
  • Possible duplicate of [JavaScript: Create and save file](https://stackoverflow.com/questions/13405129/javascript-create-and-save-file) – Guillermo Gutiérrez Jul 17 '18 at 14:29
  • You also need to understand / clarify that you actually want to write to the _server_ filesystem, not the client. – SLaks Jul 17 '18 at 14:30
  • You have a ruby backend and you want a file to written to the server? Did you write the backend app? Is it a third party app? – Strelok Jul 17 '18 at 14:33

1 Answers1

0

Generally you can't in your browser due to security concerns.

However it doesn't mean you can't get your browser to make your server to do it -> eg Server Side JavaScript Node.js.

eg using Filesystem

var fs = require('fs');
fs.writeFile("yourpath", "Hello", function(err) {
    if(err) {
        console.log(err);
    } else {
        console.log("The file was saved!");
    }
});

Some browsers do now allow File system access through the File Api:

https://developer.mozilla.org/en-US/docs/Web/API/File_and_Directory_Entries_API

see this link for a more detailed answer:

Local file access with javascript

Richrd
  • 6,722
  • 1
  • 17
  • 12
Afshin Ghazi
  • 2,784
  • 4
  • 23
  • 37
  • Some browsers now support the [File System Access API](https://web.dev/file-system-access/), which can be used to modify files on the local filesystem. – Anderson Green Feb 15 '22 at 22:37