javascript 如何从表单中获取文件内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8645369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:05:36  来源:igfitidea点击:

how do I get the file content from a form?

javascripthtml

提问by b10hazard

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 表单中获取文件的内容?这是我正在使用的示例。它所做的一切都输出类似“C:\Fake Path\namefile

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

编辑:似乎解决方案很难以这种方式实现。我想要完成的是将文件发送到我的网络服务器上的 python 脚本。这是我第一次尝试这种事情,所以欢迎提出建议。我想我可以将 python 脚本放在我的 cgi 文件夹中并使用类似的东西将值传递给它......

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

这是将文件内容发送到网络服务器而不是让 javacript 直接使用 FileReader 打开它的更好解决方案吗?

采纳答案by copy

You can actually do that with the new FileReaderObject.

您实际上可以使用新FileReader对象来做到这一点。

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)

(适用于最新版本的 Chrome 和 Firefox)

回答by Naftali aka Neal

You cannotget the contents of a file in a form without submitting the form to your server.

如果将表单提交到您的服务器,您就无法在表单中获取文件的内容。

回答by Umesh Patil

yes. Replace line of input as given below, Then it will work :)

是的。如下所示替换输入行,然后它将起作用:)

<input type="file" ACCEPT="text/html" name="whocares" id="idexample" />

回答by Morten Anderson

As Neal said - this is for submitting files to server.

正如尼尔所说 - 这是用于向服务器提交文件。

Another thing worth mentioning in regards to your existing code. Your form should use POST for submitting data. You haven't definded the method and default is GET

关于您现有的代码,另一件值得一提的事情。您的表单应使用 POST 提交数据。你还没有定义方法,默认是 GET

<form name = "form_input" enctype="multipart/form-data" method="POST>