我正在编写一个简单的 JavaScript 页面,以从用户处获取文件并将其转换为二进制或文本的数据变量。当我在按钮的单击事件处理程序中使用我的代码时,出现以下错误:
未捕获的类型错误:file.getAsText 不是 HTMLButtonElement.sendfButton.onclick 中的函数
首先,我浏览并选择一个文件,然后单击发送按钮。
这是我的 html 输入和按钮:
<tr>
<td>
<button type="button" class="control-button" id="send-file-button">SEND-FILE</button>
</td>
<td>
<input type='file' id='myfileinput' multiple><br>
<div id='output'>
</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
这是我的变量:
var sendfButton = document.getElementById("send-file-button");
var fileInput = document.getElementById("myfileinput");
Run Code Online (Sandbox Code Playgroud)
这是我的点击事件处理程序:
sendfButton.onclick = function ()
{
var files = fileInput.files;
var accept = {
binary : ["image/png", "image/jpeg"],
text : ["text/plain", "text/css", "application/xml", "text/html" , "text/txt"]
};
var file;
if (conn && conn.open)
{
console.log('in1');
//convert a file to bytes then send …Run Code Online (Sandbox Code Playgroud)