将文本文件上传到Html / JS中的输入 [英] Uploading Text File to Input in Html/JS

查看:234
本文介绍了将文本文件上传到Html / JS中的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单中的输入类型文件,我希望立即将其作为文本文件读取并用于更新文本输入的值:

I have an input type file in a form that I would like to be immediately read as a text file and used to update the value of a text input:

HTML:

<input type=file id='uploadfile' name='uploadfile' onchange="uploadCoordinates(this.form)">

JS:

function uploadCoordinates(form) {
       var file=form.uploadfile.value;
       var reader=new FileReader();
       reader.readAsText(file.value);
       form.coordinates.value=reader.result; 
}

Howveer,我收到以下错误:未捕获的异常:[例外... 无法转换JavaScript参数arg 0 [nsIDOMFileReader.readAsText]nsresult:0x80570009(NS_ERROR_XPC_BAD_CONVERT_JS)位置:JS frame :: https://portal.nersc.gov/project/als/ShirleyXAS/ShirleyXAS.js :: uploadCoordinates :: line 142data:no]

Howveer, I get the following error: uncaught exception: [Exception... "Could not convert JavaScript argument arg 0 [nsIDOMFileReader.readAsText]" nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)" location: "JS frame :: https://portal.nersc.gov/project/als/ShirleyXAS/ShirleyXAS.js :: uploadCoordinates :: line 142" data: no]

推荐答案

试试这个有错误你试图读取value.value

try this there is error you are trying to read value.value

正确的代码是

Correct code is

JS: function uploadCoordinates(form) {
       var file=form.uploadfile.value;
       var reader=new FileReader();
       reader.readAsText(file);
       form.coordinates.value=reader.result; }

这篇关于将文本文件上传到Html / JS中的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆