Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to get a file object in TypeScript from a html file-Input-Type.?

Tags:

input

public UploadFile() 
{
    //File Data
    this.filePath = $("#inputFile").val();
    var file = $("#inputFile").get(0).files[0];  
    var reader = new FileReader();
    reader.onload = function (evt) {
    var fileContent = reader.result;
    var x = fileContent.bytes;                          
}
like image 542
shrikant Veer Avatar asked Sep 14 '16 08:09

shrikant Veer


1 Answers

Your question isn't completely clear, but here's some sample code that may help. This should be valid TypeScript code, which reads a file from input element #inputFile and displays the text from it in a div with id #divMain.

$("#inputFile").on('change', null, (e) => {
    var input = <HTMLInputElement>e.target;
    var files = input.files;
    var f:File = files[0];
    var reader = new FileReader();
    var name = f.name;
    console.log("File name: " + name);
    reader.onload = function (e) {
        var target: any = e.target;
        var data = target.result;
        $("#divMain").text(data);
    };
    reader.readAsText(f);
});
like image 68
Andrew Huey Avatar answered Sep 23 '22 23:09

Andrew Huey