我如何从表单获取文件内容? [英] how do I get the file content from a form?
问题描述
如何从HTML表单获取文件的内容?这是我正在使用的一个例子。它所做的只是输出类似C:\Fake Path\\\
ameoffile
< html>
< ;头>
< script type =text / javascript>
function doSomething(){
var fileContents = document.getElementById('idexample').value;
document.getElementById('outputDiv')。innerHTML = fileContents;
}
< / script>
< / head>
< body>
< form name =form_inputenctype =multipart / form-data>
< input type =filename =whocares id =idexample/>
< button type =buttononclick =doSomething()> Enter< / button>
< / form>
< div id =outputDiv>< / div>
< / body>
< / html>
编辑:
看起来解决方案很难以这种方式实现,我试图完成的是发送一个文件到我的web服务器上的一个python脚本,这是我的第一次提示Ë尝试这种事情,所以建议是值得欢迎的。我想我可以把python脚本放在我的cgi文件夹中,并使用类似的方式将值传递给它...
/ cgi /pythonscript.py?FILE_OBJECT=fileobjecthere&OTHER_VARIABLES=whatever
这是发送文件的更好解决方案吗?内容到web服务器而不是直接使用FileReader打开它?
你可以用新的 FileReader
Object。
试试这个工作示例
函数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]);
code
$ b (适用于最新版本的Chrome和Firefox)
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 Path\nameoffile
<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?
解决方案 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)
这篇关于我如何从表单获取文件内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!