- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图从系统中获取本地文件,我进行了一些搜索并找到了一种方法,当我尝试在我的代码中实现它时,我得到了错误:
Uncaught TypeError: Cannot read property 'type' of undefined
document.getElementById('add-new-cat').addEventListener('click', handleFileSelect, false);
function handleFileSelect(evt) {
var files = evt.target.files;
if(files.type.match('image.*')) {
var reader = new FileReader();
reader.onload = (function(theFile) {
})(files);
var catIMG = reader.readAsBinaryString(files);
alert(catIMG);
}
}
<input type="file" name="cat_path_orig" id="cat-path-orig">
<button class="btn btn-primary" id="add-new-cat">add</button>
我不知道如何使用包含的文件触发函数,因为我知道它正在寻找被单击的按钮中的值
最佳答案
click
事件对象没有 files
属性。我认为您正在寻找 input type="file"
对象的 files
属性:
document.getElementById('add-new-cat').addEventListener('click', handleFileSelect, false);
function handleFileSelect(evt) {
var files = document.getElementById("cat-path-orig").files; // ****
console.log("files.length: " + files.length);
Array.prototype.forEach.call(files, function(file) {
console.log(file.name + ": " + file.type);
});
}
<input type="file" name="cat_path_orig" id="cat-path-orig">
<button class="btn btn-primary" id="add-new-cat">add</button>
一些其他注意事项:
您需要查看每个文件的 type
属性(参见上面的代码片段)。在您的示例中,只有一个(因为 input type="file"
上没有 multiple
属性),所以您可以只使用 files[0 ]
而不是我在上面使用的循环。
您对 readAsBinaryString
的使用也不正确; here's an answer with a correct example .
关于按钮上的javascript文件阅读器单击错误: undefined refference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40467981/
我会保持简短:我正在尝试循环遍历画廊的 xml 文档。我有一个应该可以工作的脚本,但没有。谁能告诉我哪里做错了? 我不想让它变得更长,因为问题很简单,并且从昨天开始就一直在思考这个问题,这是我得到的最
我正在使用 PHPExcel从 Excel 工作表中读取数据并存储在 mysql 表中,直到现在我能够上传 .xls 和 .xlsx 文件,在上传 xls 后我得到了下面的数据表结构 name
我正在构建一个在线 Rss 阅读器。我希望能够与文章标题和描述一起显示图像。 我正在使用谷歌提要 API 从 CNN ( http://rss.cnn.com/rss/edition.rss ) 读取
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 10年前关闭。 Improve this
我正在开发 BB 应用程序,我需要在其中实现 QR 阅读器或扫描仪。我知道 RIM 在 OS6 和 ZXing 中支持它的库,但实际上我需要阅读一个示例,说明如何在我的代码中实现它。 最佳答案 你可以
我将工作应用程序的 clojurescript 版本升级到 0.0-2030,突然读取器/读取字符串返回空值,例如: (js/alert (str "reader returned [" (read
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我想编写一个小应用程序,可以从任何 RSS 提要 URL 中提取 RSS 提要。如果有人能给我关于如何实现这一目标的非常基本的帮助? 我刚刚开始接触 AJAX 之类的东西,所以任何帮助将不胜感激。 谢
我已经创建了一个 RSS 阅读器,如下教程所示: http://techiedreams.com/android-rss-reader-part-3-action-bar-with-animated-
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 4 年前。
请帮我解决这个问题。我是 extJs 的新手,我需要一点帮助。我有这个代码 Ext.onReady(function() { var datesStore = new Ext.data.JsonSt
我需要一个 CSV 读取器,它将输出 NxN(加权)邻接矩阵(N 从一开始就不知道)。当然,我可以使用 strtok() 和 friend 来解析它,但是如果某些东西已经可用并且足够完整,我将不胜感激
实际上我想开发一个 EPUB 格式的图书列表(列表将从网络服务中检索)。 当选择一个项目(一本书)时,它应该会提示手机中安装了可用的 epub 阅读器。类似于“分享”将调用 SMS、FB、Twitte
我是 Java 的新手,但真的想在这方面做得更好。我正在尝试编写一个简单的 RSS 阅读器。这是代码: import java.io.*; import java.net.*; public clas
我按照一个简单的教程 (http://www.cse.nd.edu/courses/cse40814/www/RSS_Android.pdf) 将给定 URL 中的 RSS 提要读取到 ListVie
最近我一直在尝试学习如何在 Xcode 6 beta 中制作 RSS 阅读器应用程序的教程,尽管我使用的是 Xcode 6.1。我遇到了一行似乎是错误的。 完整代码为: import UIKit cl
代码使用条形码扫描仪检查条形码。Search_code 由用户(键盘)填写,insert_code 由条码扫描仪自动填写。目前,如果在条形码扫描仪值中引入两个输入,则代码可以工作,这对我来说不起作用。
是否可以通过编程方式更改 Windows 中的默认 PDF 阅读器。 例如, 如果我的机器中的默认阅读器是“Foxit”,但我需要在 C# 或 javascript 中将默认阅读器应用程序更改为“Ad
我想将 CSV 文件的每一行与其自身以及一列中的每一行进行比较。 例如,如果列值是这样的: 值_1 值_2 值_3 代码应该选择 Value_1 并将其与 Value_1(是的,也与它本身)、Valu
我有以下片段 import csv data = {} with open('data.csv', 'rb') as csvfile: spamreader = csv.reader(csvf
我是一名优秀的程序员,十分优秀!