Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
13
rated 0 times [  18] [ 5]  / answers: 1 / hits: 26261  / 13 Years ago, tue, december 27, 2011, 12:00:00

How do I get the contents of a file form a HTML form? Here is an example of what I'm working with. All it does it output something like C:Fake Pathnameoffile



<html>
<head>

<script type=text/javascript>
function doSomething(){
var fileContents = document.getElementById('idexample').value;
document.getElementById('outputDiv').innerHTML = fileContents;
}
</script>

</head>
<body >

<form name = form_input enctype=multipart/form-data>

<input type=file name=whocares id=idexample />
<button type=button onclick=doSomething()>Enter</button>

</form>

<div id=outputDiv></div>

</body>
</html>


EDIT:
It seems the solution is difficult to implement this way. What I'm trying to accomplish is sending a file to a python script on my webserver. This is my first time trying this sort of thing so suggestions are welcome. I supposes I could put the python script in my cgi folder and pass the values to it using something like...



/cgi/pythonscript.py?FILE_OBJECT=fileobjecthere&OTHER_VARIABLES=whatever


Would this be a better solution for sending file content to a webserver rather than having javacript open it directly using FileReader?


More From » html

 Answers
11

You can actually do that with the new FileReader Object.



Try this working example



function doSomething()
{
var file = document.getElementById('idexample');

if(file.files.length)
{
var reader = new FileReader();

reader.onload = function(e)
{
document.getElementById('outputDiv').innerHTML = e.target.result;
};

reader.readAsBinaryString(file.files[0]);
}
}


(works with the newest versions of Chrome and Firefox)


[#88354] Sunday, December 25, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tomas

Total Points: 165
Total Questions: 111
Total Answers: 103

Location: Maldives
Member since Tue, Dec 21, 2021
2 Years ago
;