0

I want an onload alert to show my file names from a hard drive. I had a previous question about reading files on a hard drive (Javascript to read other files in hard drive?) and with the help of Ozan Deniz's comment about a File Reader (Reading client side text file using Javascript), I figured out what will work but I needed a list of files first. So, using command line I populated a text file using

Command Line

dir/b * > files.txt

but I cannot get the readFile function to work correctly. I get the error "Failed to execute 'readAsText' on 'FileReader': The argument is not a Blob." with the following code:

Javascript

<script>
//document.getElementById('file').addEventListener('click', readFile, false);

function readFile () {
    var files = document.getElementById('file');//evt.target.files;
    var file = files[0];           
    var reader = new FileReader();
    reader.onload = function() {
        alert(this.result);            
    }
    reader.readAsText(file)
}

readFile();
</script>

HTML

<a href='files.txt' id='file'>Files</a>
Community
  • 1
  • 1
triplethreat77
  • 1,276
  • 8
  • 34
  • 69
  • How is an anchor element a file? If you want to read a file on the server, you are going to need to make an Ajax request to fetch it. – epascarello Sep 08 '14 at 14:33
  • @epascarello, it is working here without ajax but it is loading in via file instead of a link http://stackoverflow.com/questions/4950567/reading-client-side-text-file-using-javascript – triplethreat77 Sep 08 '14 at 14:39
  • A anchor input !== A file input, it will not work. And you can not set the value of a file input. If you are generating it from the server and the file lives on the server, you should fetch it via Ajax. – epascarello Sep 08 '14 at 14:40
  • It's not on a server, that's why I'm trying to get it this way. – triplethreat77 Sep 08 '14 at 14:58
  • It is impossible to read local files without the user selecting them with normal security settings. Think about what would happen if you go to any site and they just start hitting your machine and uploading files. – epascarello Sep 08 '14 at 15:00
  • Is it possible to force the value path of the input file with jQuery? – triplethreat77 Sep 08 '14 at 15:04

0 Answers0