user3491862
user3491862

Reputation: 382

How to make a JQuery routine write to a text file on a computer desktop?

I want to make a JQuery routine that can write information (append) to a text file that either exists or does not exists. If the file does not exists than it should create the file and if it does it should either append or start writing new data to the file. I think append would be the best choice for a file logger. So it must append the data to the file.

I found this code on the internet and am trying to work it around so that I can use it on my page to write information to a simple text file.

Question: How can I make the following code log to a file for download?

Below is the new code and how I read the page that was listed in the comments on how a logger in Java script should work. The code is not working and I am not really certain as to why.

I am not really certain as to how the download works either but if I can just get the logger to work I will be happy for the time being.

Code:

<!DOCTYPE html>
<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
    <script src="log4moz.js">
</head>

<script>
getLocalDirectory : function() {
  let directoryService = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
  // this is a reference to the profile dir (ProfD) now.
  let localDir = directoryService.get("ProfD", Ci.nsIFile);
  localDir.append("XULSchool");

  if (!localDir.exists() || !localDir.isDirectory()) {
    // read and write permissions to owner and group, read-only for others.
    localDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0774);
  }

  return localDir;
}

let myFile = XULSchool.getLocalDirectory();
myFile.append("someFile.txt");

let formatter = new Log4Moz.BasicFormatter();
let root      = Log4Moz.repository.rootLogger;
let logFile   = this.getLocalDirectory(); // remember this?
let appender;

logFile.append("log.txt");
root.level = Log4Moz.Level["All"];

appender       = new Log4Moz.RotatingFileAppender(logFile, formatter);
appender.level = Log4Moz.Level["All"];
root.addAppender(appender);

this._logger       = Log4Moz.repository.getLogger("XULSchool.SomeObject");
this._logger.level = Log4Moz.Level["All"];
this._logger.fatal("This is a fatal message.");
this._logger.error("This is an error message.");
this._logger.warn("This is a warning message.");
this._logger.info("This is an info message.");
this._logger.config("This is a config message.");
this._logger.debug("This is a debug message.");
this._logger.trace("This is a trace message.");

</script>

<body>

<form id="addnew">
    <input type="text"   class="A">
    <input type="text"   class="B">
    <input type="submit" value="Add">
</form>

</body>
</html>

Upvotes: 0

Views: 388

Answers (1)

Mulan
Mulan

Reputation: 135187

@Smeegs says this nicely

Imagine a world where any website can edit files on your computer

JavaScript (or jQuery) cannot touch the user's file system.

Even if you find some hacked up thing that works via ActiveXObject, you should not attempt to do this. Cross-browser support would be very narrow for this feature.

If you want to write out file, just provide the user with a download.

If this is just a means of reading/writing some kind of data, look into localstorage.

Upvotes: 5

Related Questions