如何使用angularjs 2和typescript从文件上传中读取XML内容? [英] How to read XML content from file upload using angularjs 2 and typescript?

查看:496
本文介绍了如何使用angularjs 2和typescript从文件上传中读取XML内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了以下2个内容并尝试阅读文件内容但无法获取内容并收到文件等错误



< ; input type ='file'accept ='text / xml'(change)='openFile(fileupload)'> 

openFile(fileupload){
let input = fileupload.target ;
for(var index = 0; index< input.files.length; index ++){
let reader = new FileReader();
reader.onload =()=> {
//这个'text'是文件的内容
var text = reader.result;
}
reader.readAsText(input.files [index]);
};





我尝试过:



我尝试过这些



< input type ='file'accept ='text / xml'(更改) = '中openFile(文件上传)'> 

openFile(fileupload){
let input = fileupload.target;
for(var index = 0; index< input.files.length; index ++){
let reader = new FileReader();
reader.onload =()=> {
//这个'text'是文件的内容
var text = reader.result;
}
reader.readAsText(input.files [index]);
};





错误:

无法读取未定义的属性'文件'





关于如何获取从文件上传控件上传的任何文件的内容,请你帮忙。

解决方案

将您的输入更改为:

< input type ='file'accept ='text / xml'onchange ='openFile(this)' > 



除了属性中的语法错误外,您还尝试传递一个名为 fileupload的全局变量你的功能。但是没有这样的变量。



你还需要改变你的功能,因为输入直接传递给函数:

 openFile(fileupload){
let input = fileupload; //删除:.target;
...
};


I have created the below 2 things and try to read file content but not able to get content and getting errors like files

<input type='file' accept='text/xml' (change)='openFile(fileupload)'">

openFile(fileupload) {
    let input = fileupload.target;
    for (var index = 0; index < input.files.length; index++) {
        let reader = new FileReader();
        reader.onload = () => {
            // this 'text' is the content of the file
            var text = reader.result;
        }
        reader.readAsText(input.files[index]);
    };



What I have tried:

I have tried with these

<input type='file' accept='text/xml' (change)='openFile(fileupload)'">

openFile(fileupload) {
    let input = fileupload.target;
    for (var index = 0; index < input.files.length; index++) {
        let reader = new FileReader();
        reader.onload = () => {
            // this 'text' is the content of the file
            var text = reader.result;
        }
        reader.readAsText(input.files[index]);
    };



errors:

Cannot read property 'files' of undefined



Could you please help on this that how to get the content of any file which has uploaded from file upload control.

解决方案

Change your input to:

<input type='file' accept='text/xml' onchange='openFile(this)'">


Apart from the syntax error in your attribute, you're trying to pass a global variable called fileupload to your function. But no such variable exists.

You'll also need to change your function, since the input is passed directly to the function:

openFile(fileupload) {
    let input = fileupload; // Remove: .target;
    ...
};


这篇关于如何使用angularjs 2和typescript从文件上传中读取XML内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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