download.js 506 B

1234567891011121314151617181920
  1. var createUrl = require('./createUrl');
  2. exports = function(data, name, type) {
  3. type = type || 'text/plain';
  4. var el = document.createElement('a');
  5. el.setAttribute(
  6. 'href',
  7. createUrl(data, {
  8. type: type
  9. })
  10. );
  11. el.setAttribute('download', name);
  12. el.addEventListener('click', function(e) {
  13. e.stopImmediatePropagation();
  14. });
  15. document.body.appendChild(el);
  16. el.click();
  17. document.body.removeChild(el);
  18. };
  19. module.exports = exports;