Javascript Write to File System

依然范特西╮ 提交于 2021-02-08 12:02:15

问题


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!


回答1:


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



来源:https://stackoverflow.com/questions/51383901/javascript-write-to-file-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!