openFile.js 847 B

1234567891011121314151617181920212223242526
  1. exports = function() {
  2. var options =
  3. arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  4. return new Promise(function(resolve) {
  5. var input = document.createElement('input');
  6. input.style.position = 'fixed';
  7. input.style.bottom = '0';
  8. input.style.left = '0';
  9. input.style.visibility = 'hidden';
  10. input.setAttribute('type', 'file');
  11. if (options.accept) {
  12. input.setAttribute('accept', options.accept);
  13. }
  14. if (options.multiple) {
  15. input.setAttribute('multiple', '');
  16. }
  17. document.body.appendChild(input);
  18. input.addEventListener('change', function() {
  19. document.body.removeChild(input);
  20. resolve(input.files);
  21. });
  22. input.click();
  23. });
  24. };
  25. module.exports = exports;