Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript json upload

function upload() {
    const input = document.createElement('input');
    input.style.visibility = 'hidden';
    input.type = 'file';
    input.onchange = e => { 
        // getting a hold of the file reference
        const file = e.target.files[0];
        // setting up the reader
        const reader = new FileReader();
        reader.readAsText(file,'UTF-8');
        // here we tell the reader what to do when it's done reading...
        reader.onload = readerEvent => {
            let content = readerEvent.target.result; // this is the content!
            try {
                // Try to read json
                // noinspection JSCheckFunctionSignatures
                objs = JSON.parse(content);
            } catch (e) {
                objs = {};
            }
        }
    }
    input.click();
}
 
PREVIOUS NEXT
Tagged: #javascript #json #upload
ADD COMMENT
Topic
Name
7+6 =