admin管理员组

文章数量:1426959

<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?

<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?

Share Improve this question edited Feb 18, 2023 at 16:03 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Dec 5, 2012 at 22:14 Animesh PandeyAnimesh Pandey 6,02814 gold badges71 silver badges131 bronze badges 8
  • Do you want the content of the actual file in your text-area or just the filename of the file from the input file control? – Hanlet Escaño Commented Dec 5, 2012 at 22:17
  • 1 Are you confusing php with javascript? Javascript, as a clientside language does not support diskoperations such as fileopen. – Henrik Andersson Commented Dec 5, 2012 at 22:18
  • this was already answered before stackoverflow./a/8137303/1781209 – jxs Commented Dec 5, 2012 at 22:24
  • @João Oliveira I dont wish to use AciveX, that is why I asked !!! – Animesh Pandey Commented Dec 5, 2012 at 22:26
  • 1 Your CopyMe function is run client-side, so the php code is just a string and will not be executed. you need to submit the form/file to your server to be able to print the contents using php – NickSlash Commented Dec 5, 2012 at 22:55
 |  Show 3 more ments

2 Answers 2

Reset to default 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/wAJe4/1/

it's documented at Mozilla Developer Nework for example

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

本文标签: dom eventsHow to load text from a file to text area using JavaScript using input type fileStack Overflow