Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I get the filename of the file I dragged and dropped? PHP Javascript

I know nothing about drag and dropping files, but I found this nifty little code that allows me to drag a text file into a textarea. It works great. I was wondering if it’s possible to get the filename of the file I dropped and save it to a php variable? I would need the filename after the form is submitted.

function dropfile(file) {
  var reader = new FileReader();
  reader.onload = function(e) {
    area.value = e.target.result;
  };
  reader.readAsText(file, 'UTF-8');
}

area.ondrop = function(e) {
  e.preventDefault();
  var file = e.dataTransfer.files[0];
  dropfile(file);
};
<form  method='post' action='' id='theform'>
<textarea id='area' name='data'></textarea>
</form>
<button id=sub onclick='theform.submit()'>SUBMIT</button>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can make use of File.prototype.name property in your drop event like so:

function dropfile(file) {
  var reader = new FileReader();
  reader.onload = function(e) {
    area.value = file.name + '\n' + e.target.result;
  };
  reader.readAsText(file, 'UTF-8');
}

area.ondrop = function(e) {
  e.preventDefault();
  var file = e.dataTransfer.files[0];
  dropfile(file);
};
<form method='post' action='' id='theform'>
  <textarea id='area' name='data'></textarea>
</form>
<button id=sub onclick='theform.submit()'>SUBMIT</button>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading