简体   繁体   中英

How to load text from a file to text area using javascript using input type file?

<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 codo for that, I dont 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 ... ???

@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

如果在浏览器中运行此程序,则无法使用JavaScript在客户端计算机上读取文件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM