browserView.mjs

  1. /**
  2. * 前端瀏覽器以分頁開啟指定數據如文字、圖片、影片、pdf等
  3. *
  4. * Unit Test: {@link https://github.com/yuda-lyu/wsemi/blob/master/test/browserView.test.mjs Github}
  5. * @memberOf wsemi
  6. * @param {*} data 輸入數據
  7. * @param {String} type 輸入數據MIME Type字串,例如'application/pdf'或'text/plain'等,詳見[https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types]
  8. * @example
  9. * need test in browser
  10. *
  11. * let text = 'abc'
  12. * browserView(u8a, 'text/plain')
  13. *
  14. * let u8a = new Uint8Array([66, 97, 115])
  15. * browserView(u8a, 'application/pdf')
  16. *
  17. */
  18. function browserView(data, type) {
  19. //blob
  20. let blob = new Blob([data], { type })
  21. //IE11無法支援createObjectURL
  22. try {
  23. //url
  24. let url = window.URL.createObjectURL(blob)
  25. //open
  26. window.open(url)
  27. }
  28. catch (err) {
  29. console.log('window.URL.createObjectURL is not support for IE11', err)
  30. }
  31. }
  32. export default browserView