-2

I am new in javascript and was trying to read and write in local txt file so I did as I found in the web but for some reason it stucks in new File instantiation:

<script type="text/javascript">
var txtFile = "d:/test.txt"
alert('point 1');
var file = new File(txtFile);
alert('point 2');
</script>

It prints only 'point 1' string. Thanks.

JamesB
  • 569
  • 1
  • 9
  • 31
  • `File` constructor requires two parameters – guest271314 May 02 '16 at 02:44
  • Writing to the local file system is much more complicated than that. Look up some tutorials! – Robert Moskal May 02 '16 at 02:50
  • I was not saying that I was trying to write in a file in this particular piece of code, I was saying that it stucks in the new file instantiation, this is first part, understand a phrase is more complicated than that. – JamesB May 02 '16 at 18:48

1 Answers1

2

new File() constructor requires at least two parameters; first parameter an array containing String, ArrayBufferView, ArrayBuffer Blob or another File object; the second the file name. You cannot specify a download directory for files using javascript, but you can set a download directory at browser settings or preferences. You can also utilize download attribute at a element to set file name at Save File dialog.

I am new in javascript and was trying to read and write in local txt file

You could use <input type="file"> element to retrieve test.txt, and edit .txt file at <textarea> before re-saving with same file name. See also Edit, save, self-modifying HTML document; format generated HTML, JavaScript , How to Write in file (user directory) using JavaScript?

<script type="text/javascript">
  var txtFile = "test.txt";
  var text = "abc";
  var file = new File([text], txtFile);
  console.log(file);
  var a = document.createElement("a");
  a.download = file.name;
  a.href = URL.createObjectURL(file);
  document.body.appendChild(a);
  a.innerHTML = file.name;
  a.onclick = function() {
    this.parentElement.removeChild(this)
  }
</script>
Community
  • 1
  • 1
guest271314
  • 1
  • 15
  • 104
  • 177
  • It generates one file with the text and erases every text if the file exists, but how to recover the text to an string? – JamesB May 02 '16 at 18:54
  • @JamesB _"It generates one file with the text and erases every text if the file exists, but how to recover the text to an string?"_ Aru trying to concatenate additional text content to existing file, then save file? – guest271314 May 03 '16 at 00:20
  • Sorry you don't understand, you code create a file and the link to download it, I want to read and write in local machine. – JamesB May 05 '16 at 01:50
  • @JamesB _"I want to read and write in local machine"_ This is the closest have been able to achieve to direct read and write in local machine http://stackoverflow.com/questions/36098129/how-to-write-in-file-user-directory-using-javascript/ . Note, when a file is downloaded, it is written to local machine. You could use `` to read file from local machine, modify file, then download file set to same name to overwrite existing file see http://stackoverflow.com/questions/30563157/edit-save-self-modifying-html-document-format-generated-html-javascript – guest271314 May 05 '16 at 02:57