Alba
Alba

Reputation: 95

Read text file context without save it on server - Asp classic

I would like to create a form where I could select a file and read it without save the file on server side.

Does anyone knows how to do that?

Upvotes: 0

Views: 431

Answers (1)

Ricardo Pontual
Ricardo Pontual

Reputation: 3757

You can user FileReader from javascript, here is a good example: http://www.htmlgoodies.com/beyond/javascript/read-text-files-using-the-javascript-filereader.html#fbid=zc2XllkeDXm

More here: http://www.javascripture.com/FileReader

Basically, you need to attach the change of your input file to a function and read the file using FileReader

<input type="file" id="fileinput" multiple />
<script type="text/javascript">
  function readMultipleFiles(evt) {
    //Retrieve all the files from the FileList object
    var files = evt.target.files; 

    if (files) {
        for (var i=0, f; f=files[i]; i++) {
              var r = new FileReader();
            r.onload = (function(f) {
                return function(e) {
                    var contents = e.target.result;
                    alert( "Got the file.n" 
                          +"name: " + f.name + "n"
                          +"type: " + f.type + "n"
                          +"size: " + f.size + " bytesn"
                          + "starts with: " + contents.substr(1, contents.indexOf("n"))
                    ); 
                };
            })(f);

            r.readAsText(f);
        }   
    } else {
          alert("Failed to load files"); 
    }
  }

  document.getElementById('fileinput').addEventListener('change', readMultipleFiles, false);
</script>

Upvotes: 1

Related Questions