Import Text File Using Javascript

后端 未结 2 435
后悔当初
后悔当初 2021-01-06 05:29

I am currently working on an application that handles a fairly large amount of data. Currently, I\'ve hard-coded those values into the Javascript iself (defining global arra

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-06 06:01

    Here is native javascript solution without libraries.

    http://caniuse.com/xhr2

    As it's async you have to create 2 functions

    one to read and another one to show/modify or whatever

    function read(textFile){
        var xhr=new XMLHttpRequest;
        xhr.open('GET',textFile);
        xhr.onload=show;
        xhr.send()
    }
    
    function show(){
        var pre=document.createElement('pre');
        pre.textContent=this.response;
        document.body.appendChild(pre)
    }
    
    read('text.txt');
    

    if you work alot with external files i suggest also to take a look at the new javascript classes new FileReader() and window.requestFileSystem()

    where new FileReader() has now a little more support, also on mobile devices

    and from what i know window.requestFileSystem() has almost no support.. but you can handle files that are various gb large.. using Chrome.

提交回复
热议问题