简体   繁体   English

如何使用输入类型文件使用JavaScript将文件中的文本加载到文本区域?

[英]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 codo,但我不知道它是否正确

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

Following was the javascript: 以下是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 @ apanimesh061,您必须使用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/ http://jsfiddle.net/wAJe4/1/

it's documented at Mozilla Developer Nework for example 例如在Mozilla Developer Nework上有记录

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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