1
<script type="text/javascript">
function CopyMe(oFileInput) {
var filePath = oFileInput.value;
    fh = fopen(filePath, 0);
    if (fh!=-1) {
        length = flength(fh);
        str = fread(fh, length);
        fclose(fh);
    }
document.getElementByID('myText').innerHTML = filePath;
}
</script>
<input type="file" onchange="CopyMe(this);"/>
<textarea id="myText"></textarea>

I do get any output/change in the text area! What should I do? Please help!

I used the following PHP code for that, I don't know whether it is correct:

<?php
function Read($file){
echo file_get_contents($file);
};
?>

Following was the JavaScript:

    function CopyMe(oFileInput) {
    var filePath = oFileInput.value;
    document.getElementByID('text-area3').innerHTML = "<?php Read(filePath);?>";
    }

Any suggestions?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Animesh Pandey
  • 5,900
  • 13
  • 64
  • 130

2 Answers2

4

@apanimesh061 you have to use the FileReader api

document.getElementById('files').addEventListener('change', CopyMe, false);
function CopyMe(evt) {
    var file = evt.target.files[0];
    if (file) {
        var reader = new FileReader();
        reader.readAsDataURL(file)
    }
};

http://jsfiddle.net/wAJe4/1/

it's documented at Mozilla Developer Nework for example

jxs
  • 457
  • 4
  • 9
1

If you are running this in a browser you can't read files on the client machine using JavaScript.

Tad
  • 934
  • 6
  • 10