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
2 Answers
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 events - How to load text from a file to text area using JavaScript using input type file? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745447030a2658710.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论