Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function() {
- 'use strict';
- function fileCallback(fileData) {
- console.log(fileData);
- }
- let background = document.createElement('div');
- background.style.position = 'fixed';
- background.style.left = '0';
- background.style.top = '0';
- background.style.width = '100%';
- background.style.height = '100%';
- background.style.zIndex = '99';
- background.style.background = 'rgba(0,0,0,0.4)';
- let dialFrame = document.createElement('div');
- dialFrame.style.position = 'absolute';
- dialFrame.style.padding = '12px';
- dialFrame.style.left = '50%';
- dialFrame.style.top = '50%';
- dialFrame.style.transform = 'translate(-50%, -50%)';
- dialFrame.style.background = 'white';
- let promptButton = document.createElement('input');
- promptButton.setAttribute('type', 'file');
- promptButton.setAttribute('accept', '.txt');
- promptButton.style.display = 'block';
- let cancelButton = document.createElement('button');
- cancelButton.setAttribute('type', 'button');
- cancelButton.innerText = 'Cancel';
- cancelButton.style.display = 'block';
- document.body.appendChild(background);
- background.appendChild(dialFrame);
- dialFrame.appendChild(promptButton);
- dialFrame.appendChild(cancelButton);
- promptButton.addEventListener('change', buttonChange);
- cancelButton.addEventListener('click', cancelClick);
- function removeListeners() {
- promptButton.removeEventListener('change', buttonChange);
- cancelButton.removeEventListener('click', cancelClick);
- }
- function buttonChange(evt) {
- let fread = new FileReader();
- fread.addEventListener('load', () => {
- // return found value to callback for processing
- fileCallback(fread.result);
- }, {once: true});
- // here you can read as something else if needed
- fread.readAsText(evt.target.files[0]);
- console.log('file was read');
- removeListeners();
- background.remove();
- }
- function cancelClick(evt) {
- console.log('dialog was canceled');
- removeListeners();
- background.remove();
- }
- }());
Advertisement
Add Comment
Please, Sign In to add comment