投射document.getElementById以访问TypeScript中的文件 [英] Casting document.getElementById to access files in TypeScript

查看:709
本文介绍了投射document.getElementById以访问TypeScript中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对angularjs很陌生。我无法在线找到解决方案。
我想从文件中读取文本。我试图按照这个例子。
FileReader未使用AngularJS正确加载文件



但是,由于我使用的是TypeScript,因此我收到此错误信息

 <$ c $ 。C>的document.getElementById( '的FileInput')文件[0]; 

错误:
属性'文件'在'HTMLElement'类型上不存在。



请向我建议我如何使用这些文件。

解决方案

这个例子应该可以正常工作,也许你在那里做错了什么。然而,你可以像这样测试你的代码,用 fileChanged(event)替换 fileChanged(),然后获取文件通过 event.target.files



function fileChanged(event){var target = event .target || event.srcElement;的console.log(target.files); console.log(document.getElementById('fileInput')。files);}

 < DIV> < input ng-model =csvonchange =fileChanged(event)type =fileid =fileInput/>< / div>  


I am very new to angularjs. I wasn't able to find the solution online. I would like to read the text from a file. I was trying to follow this example. FileReader not loading file properly using AngularJS

However, since I'm using TypeScript, I receive this error for this line

document.getElementById('fileInput').files[0];

Error: Property 'files' does not exist on type 'HTMLElement'.

Please suggest me how I can cast to use the files.

解决方案

This should work fine from that example, maybe you're doing something wrong there. However, you may test your code like this, replace fileChanged() by fileChanged(event) and then get the files by event.target.files

function fileChanged(event) {
  var target = event.target || event.srcElement;
  console.log(target.files);
  console.log(document.getElementById('fileInput').files);
}

<div>
  <input ng-model="csv"
            onchange="fileChanged(event)"
            type="file" id="fileInput"/>
</div>

这篇关于投射document.getElementById以访问TypeScript中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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