-1

I am trying to figure out how to write a file path to a text file in javascript/html, i.e. in a browser without a server. I have the following piece of html code:

<label class="label">File input</label>
<label for="file" class="input input-file">
<div class="button"><input type="file" id="file" onchange="this.parentNode.nextSibling.value = this.value">Search</div><input type="text" readonly>
</label>

<button type="submit" class="button" onclick=???>Submit</button>

This code already displays the file path in a textfield; however, the main goal is to write this path to a .txt file after clicking submit. I searched the web, but the available scripts are quite complicated. Is there a simple script that enables me to do this? The name of the text file is fixed.

Any help would be very much appreciated.

SWDQ
  • 41
  • 1
  • 1
  • 2
  • Make an ajax call on button click . Send the name to server side write the txt file . – Deepak Aug 09 '17 at 11:42
  • 1
    JS works on the client-side and does not have write access to a filesystem. Imagine if websites can randomly create files on your computer: that wouldn't be fun, would it? You need a server-side language to do it, e.g. Node.js (JS that runs on the server), PHP, etc. – Terry Aug 09 '17 at 11:42

2 Answers2

0

Simply you have to call one ajax on click button, and just add below code in that php file.

file_put_contents('logs.txt', "\n\n ====LOG Generated on... Date[" . date('h:i:s a d-m-Y') . "] ====== \n\n", FILE_APPEND | LOCK_EX);
Nitin Vaghani
  • 297
  • 4
  • 14
-1

You cannot edit files on the client side where JS and HTML lie. What you could do is create a .txt file and then force download it. You can see further details at this question.

Dimitris Damilos
  • 2,363
  • 5
  • 23
  • 46
  • While you are correct that this is an answer to the question, it would be better if you just flagged questions that are duplicates of other questions on Stack Overflow. Find a "flag" link underneath the tags; pick the "duplicate" option. This helps keep everything clean, and ensures that all the important information is collected in one place. – Cody Gray - on strike Aug 09 '17 at 13:10