Typescript file upload validation











up vote
0
down vote

favorite












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. How to resolve this?



I have commented the error for the following line of code.




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




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.....")

}
}









share|improve this question
























  • What exactly are you trying to achieve here?
    – SiddAjmera
    Nov 9 at 8:11










  • I'm trying to upload a file and after submitting trying to get the file name, file size and file type and in the other part trying to get the dropdown value.
    – Jijo Robin
    Nov 9 at 8:16










  • I think you use a bad approach. In angular batter use change event and handle this event. And then get the file from event There you can find a good example stackoverflow.com/questions/47936183/angular-file-upload
    – Roman Skydan
    Nov 9 at 8:20















up vote
0
down vote

favorite












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. How to resolve this?



I have commented the error for the following line of code.




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




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.....")

}
}









share|improve this question
























  • What exactly are you trying to achieve here?
    – SiddAjmera
    Nov 9 at 8:11










  • I'm trying to upload a file and after submitting trying to get the file name, file size and file type and in the other part trying to get the dropdown value.
    – Jijo Robin
    Nov 9 at 8:16










  • I think you use a bad approach. In angular batter use change event and handle this event. And then get the file from event There you can find a good example stackoverflow.com/questions/47936183/angular-file-upload
    – Roman Skydan
    Nov 9 at 8:20













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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. How to resolve this?



I have commented the error for the following line of code.




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




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.....")

}
}









share|improve this question















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. How to resolve this?



I have commented the error for the following line of code.




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




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.....")

}
}






angular html5 typescript file-upload angular6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 8:18

























asked Nov 9 at 8:04









Jijo Robin

555




555












  • What exactly are you trying to achieve here?
    – SiddAjmera
    Nov 9 at 8:11










  • I'm trying to upload a file and after submitting trying to get the file name, file size and file type and in the other part trying to get the dropdown value.
    – Jijo Robin
    Nov 9 at 8:16










  • I think you use a bad approach. In angular batter use change event and handle this event. And then get the file from event There you can find a good example stackoverflow.com/questions/47936183/angular-file-upload
    – Roman Skydan
    Nov 9 at 8:20


















  • What exactly are you trying to achieve here?
    – SiddAjmera
    Nov 9 at 8:11










  • I'm trying to upload a file and after submitting trying to get the file name, file size and file type and in the other part trying to get the dropdown value.
    – Jijo Robin
    Nov 9 at 8:16










  • I think you use a bad approach. In angular batter use change event and handle this event. And then get the file from event There you can find a good example stackoverflow.com/questions/47936183/angular-file-upload
    – Roman Skydan
    Nov 9 at 8:20
















What exactly are you trying to achieve here?
– SiddAjmera
Nov 9 at 8:11




What exactly are you trying to achieve here?
– SiddAjmera
Nov 9 at 8:11












I'm trying to upload a file and after submitting trying to get the file name, file size and file type and in the other part trying to get the dropdown value.
– Jijo Robin
Nov 9 at 8:16




I'm trying to upload a file and after submitting trying to get the file name, file size and file type and in the other part trying to get the dropdown value.
– Jijo Robin
Nov 9 at 8:16












I think you use a bad approach. In angular batter use change event and handle this event. And then get the file from event There you can find a good example stackoverflow.com/questions/47936183/angular-file-upload
– Roman Skydan
Nov 9 at 8:20




I think you use a bad approach. In angular batter use change event and handle this event. And then get the file from event There you can find a good example stackoverflow.com/questions/47936183/angular-file-upload
– Roman Skydan
Nov 9 at 8:20












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










There are a lot of issues with your code. You're using Vanilla JavaScript instead of leveraging the Angular Syntax.




  1. The change on the File Input can be tracked using (change) and passing an $event Object to the Change Handler.


  2. You can use [(ngModel)] to get the value of the selected option from the dropdown.


  3. It's not advisable to use document to access the DOM and make changes to it or show data to it. You should use the String Interpolation Syntax({{}}) instead.



Here's a Sample StackBlitz for your ref.



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.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53221884%2ftypescript-file-upload-validation%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    There are a lot of issues with your code. You're using Vanilla JavaScript instead of leveraging the Angular Syntax.




    1. The change on the File Input can be tracked using (change) and passing an $event Object to the Change Handler.


    2. You can use [(ngModel)] to get the value of the selected option from the dropdown.


    3. It's not advisable to use document to access the DOM and make changes to it or show data to it. You should use the String Interpolation Syntax({{}}) instead.



    Here's a Sample StackBlitz for your ref.



    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.






    share|improve this answer

























      up vote
      2
      down vote



      accepted










      There are a lot of issues with your code. You're using Vanilla JavaScript instead of leveraging the Angular Syntax.




      1. The change on the File Input can be tracked using (change) and passing an $event Object to the Change Handler.


      2. You can use [(ngModel)] to get the value of the selected option from the dropdown.


      3. It's not advisable to use document to access the DOM and make changes to it or show data to it. You should use the String Interpolation Syntax({{}}) instead.



      Here's a Sample StackBlitz for your ref.



      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.






      share|improve this answer























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        There are a lot of issues with your code. You're using Vanilla JavaScript instead of leveraging the Angular Syntax.




        1. The change on the File Input can be tracked using (change) and passing an $event Object to the Change Handler.


        2. You can use [(ngModel)] to get the value of the selected option from the dropdown.


        3. It's not advisable to use document to access the DOM and make changes to it or show data to it. You should use the String Interpolation Syntax({{}}) instead.



        Here's a Sample StackBlitz for your ref.



        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.






        share|improve this answer












        There are a lot of issues with your code. You're using Vanilla JavaScript instead of leveraging the Angular Syntax.




        1. The change on the File Input can be tracked using (change) and passing an $event Object to the Change Handler.


        2. You can use [(ngModel)] to get the value of the selected option from the dropdown.


        3. It's not advisable to use document to access the DOM and make changes to it or show data to it. You should use the String Interpolation Syntax({{}}) instead.



        Here's a Sample StackBlitz for your ref.



        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 8:18









        SiddAjmera

        10.6k21137




        10.6k21137






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53221884%2ftypescript-file-upload-validation%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff