5

I don't know if it's possible but here's what I would like to achieve. I would want to be able to load a JSON file using a file input, edit it in a web page and then save the changes is the initial file. I know that for security reason the browser doesn't have full access to the disk but I was wondering if there was a way to allow updates for a specific file.

In a nutshell, the flow would be

  1. Load the file
  2. Edit it
  3. Save the changes (rewriting the initial one)

I don't care about browser compatibility, so if the solution is based on a specific brower's API, it's good enough for me.

Also, I know about the download attribute, but I'm trying to avoid the "normal" download flow (popup or the file being thrown is the Downloads folder).

Thanks in advance !

Pascal Boutin
  • 1,220
  • 2
  • 9
  • 22
  • _"Also, I know about the `download` attribute, but I'm trying to avoid the "normal" download flow "_ Is requirement to not utilize `download` attribute ? , or "Save File" dialog ? How would , or should, file be saved ? See http://stackoverflow.com/questions/30563157/edit-save-self-modifying-html-document-format-generated-html-javascript – guest271314 Dec 02 '15 at 03:41
  • Is this something for the public to use, or just for you personally? – Matt Way Dec 02 '15 at 03:42
  • "How would , or should, file be saved" The same way "Save" button work in MS Word for instance. I want to save in the initially loaded file. Think for it as the different between "Save as" and "Save". Just for me at the moment, but if I can get it to work, it would be nice to able to share it :) – Pascal Boutin Dec 02 '15 at 03:46
  • @PascalBoutin Tried `html` , `js` at link ? Requirement should be possible using `input type="text"` or `textarea` , `input type="file"` elements – guest271314 Dec 02 '15 at 03:54
  • 1
    Well, I know that the saving process can be done via the saving dialog, what I'm exploring right now, is the possibility to have both "Save" and "Save as" in my app, not just "Save as". – Pascal Boutin Dec 02 '15 at 04:08
  • _"what I'm exploring right now, is the possibility to have both "Save" and "Save as""_ User has option to adjust file name at "Save File" dialog . Cannot force user to download file with a specific filename – guest271314 Dec 02 '15 at 04:17
  • Can you show some sample json file, you can't do it for word like document. – Sudipta Kumar Maiti Dec 02 '15 at 04:17

3 Answers3

4

var input = document.querySelector("input[type=file]");
var text = document.querySelector("textarea");
var button = document.querySelector("input[type=button]");
var name;

input.onchange = function(e) {
  var reader = new FileReader();
  reader.onload = function(event) {
    text.value = event.target.result;
    button.disabled = false;
  }
  name = e.target.files[0].name;
  reader.readAsText(new Blob([e.target.files[0]], {
    "type": "application/json"
  }));
}

button.onclick = function(e) {
  e.preventDefault();
  var blob = new Blob([text.value], {
    "type": "application/json"
  });
  var a = document.createElement("a");
  a.download = name;
  a.href = URL.createObjectURL(blob);
  document.body.appendChild(a);
  a.click();
  text.value = "";
  input.value = "";
  button.disabled = true;
  document.body.removeChild(a);
}
textarea {
  white-space: pre;
  width: 400px;
  height: 300px;
}
<form>
  <input type="file" />
  <br />
  <textarea></textarea>
  <br />
  <input type="button" disabled="true" value="Save" />
</form>
guest271314
  • 1
  • 15
  • 104
  • 177
  • 1
    this is the closest to what I'm trying to achieve, but it is not rewriting the initial file. if it is not possible at all, your solution will be accepted... initialy, it was more like an "exploration" question – Pascal Boutin Dec 02 '15 at 04:48
  • _"but it is not rewriting the initial file"_ ? "Save File" dialog may append a `(1)` to original file name , perhaps to prevent user from overwriting file, while can be removed at "Save File" dialog. Following removing `(1)` from prospective file name, user may again be prompted with `"A file named "abc.json" already exists. The file already exists in "folder". Replacing it will overwrite its contents."` . To prevent user from overwriting a file in error ? If maintain open editor during process , edited file should be updated in both editor and filesystem when click "Save" – guest271314 Dec 02 '15 at 04:54
1

Consider looking into FileSystem. It's only in Chrome at present and not likely to be supported in other browsers.

Lance Harper
  • 2,216
  • 14
  • 11
  • 1
    If I understand well, it is like a "virtual" file system. Is there a way to load a physical file in that FileSystem ? – Pascal Boutin Dec 02 '15 at 03:59
0

Locale storage? Stores key value pairs that will persist -variable limitations across browsers, but the general idea is supported by Chrome, Firefox, Safari, Opera, IE. Things are stored as strings, so you could store json type information as a value, rather than breaking your json into lots of key/value items.

This is not the most secure way of doing this, but would probably be fine for preferences and even application state, if you don't mind there being a potential for something client side to tweak values.

If a user wants to save this, then you invoke the download/save file option.

Matiu Carr
  • 298
  • 1
  • 4
  • 1
    I want to write some sort of online editor, and I think that it would be pretty annoying to prompt the save dialog to the user everytime he wants to save his progress, but I agree that the LocalStorage could be an efficient buffer at runtime that would be crash-proof. – Pascal Boutin Dec 02 '15 at 04:01