简体   繁体   English

打字稿文件上传验证

[英]Typescript file upload validation

I'm trying to upload a file but i get an error for the following code. 我正在尝试上传文件,但以下代码出现错误。 The error is property does not exist on type HTML Element . 错误是属性在HTML Element类型上不存在 How to resolve this? 如何解决呢?

I have commented the error for the following line of code. 我已针对以下代码行注释了该错误。

component.html component.html

<input type="file" name="fileToUpload" id="fileToUpload" onchange="fileSelected();"/>

   <ul>
     <label>Select a Module Name</label>
     <select id = "ModuleDropDown">
       <option value="">Select</option>
       <option value="Recuirtmnet">Recuirtmnet</option>
       <option value="Talent" selected="selected">Talent</option>
       <option value="Attrition">Attrition</option>
       <option value="Performance">Performance</option>
       <option value="Survey">Survey</option>
      </select>
     </ul>

    <div id="fileName"></div>
    <div id="fileSize"></div>
    <div id="fileType"></div>

component.ts component.ts

fileSelected() {

    //Property 'files' does not exist on type 'HTMLElement'
    let file = document.getElementById('fileToUpload').files[0]; 

    if (file) {
        let fileSize = 0;
        if (file.size > 1024 * 1024)
            this.fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB';
        else
            this.fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';

        document.getElementById('fileName').innerHTML = 'Name: ' + file.name;
        document.getElementById('fileSize').innerHTML = 'Size: ' + fileSize;
        document.getElementById('fileType').innerHTML = 'Type: ' + file.type;
        let dropDown = document.getElementById("ModuleDropDown");


        //Property 'options' does not exist on type 'HTMLElement'.
        //Property 'selectedIndex' does not exist on type 'HTMLElement'
        let dpVal = dropDown.options[dropDown.selectedIndex].value;

        let init_params = {};
        this.init_params.action = 'prepare';
        this.init_params.file_name = file.name;
        this.init_params.file_size = fileSize;
        this.init_params.moduleName = dpVal;

        ws.send(JSON.stringify(init_params))
        console.log("sending init params.....")

    }
}

There are a lot of issues with your code. 您的代码有很多问题。 You're using Vanilla JavaScript instead of leveraging the Angular Syntax. 您使用的是Vanilla JavaScript,而不是利用Angular语法。

  1. The change on the File Input can be tracked using (change) and passing an $event Object to the Change Handler. 可以使用(change)跟踪文件输入上的(change) ,并将$event对象传递给更改处理程序。

  2. You can use [(ngModel)] to get the value of the selected option from the dropdown. 您可以使用[(ngModel)]从下拉列表中获取所选选项的值。

  3. It's not advisable to use document to access the DOM and make changes to it or show data to it. 建议不要使用document来访问DOM并对其进行更改或向其中显示数据。 You should use the String Interpolation Syntax( {{}} ) instead. 您应该改用字符串插值语法( {{}} )。

Here's a Sample StackBlitz for your ref. 这是供您参考的示例StackBlitz

Select an Option and then Upload a File to see the Selected File Details on the UI and the Selected Dropdown Option on the console. 选择一个选项,然后上载文件以在UI上查看“选定文件详细信息”,并在控制台上看到“选定下拉菜单”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM