Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. function saveTextAsFile(fileNameToSaveAs, textToWrite) {
  2. var textFileAsBlob = new Blob([textToWrite], {
  3. type: 'text/plain'
  4. });
  5.  
  6. var downloadLink = document.createElement("a");
  7. downloadLink.download = fileNameToSaveAs;
  8. downloadLink.innerHTML = "Download File";
  9.  
  10. if (true) { //window.webkitURL !== null) {
  11. // Chrome allows the link to be clicked
  12. // without actually adding it to the DOM.
  13. downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
  14. } else {
  15. // Firefox requires the link to be added to the DOM
  16. // before it can be clicked.
  17. downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
  18. downloadLink.onclick = destroyClickedElement;
  19. downloadLink.style.display = "none";
  20. document.body.appendChild(downloadLink);
  21. }
  22.  
  23. downloadLink.click();
  24. }
  25.  
  26. downloadLink.click();
  27.  
  28. SCRIPT5: Access is denied.
  29.  
  30. window.navigator.msSaveBlob(blob, 'file.txt');
  31.  
  32. window.navigator.msSaveOrOpenBlob(blob, 'file.txt');
  33.  
  34. function saveTextAsFile(fileNameToSaveAs, textToWrite) {
  35. var ie = navigator.userAgent.match(/MSIEs([d.]+)/),
  36. ie11 = navigator.userAgent.match(/Trident/7.0/) && navigator.userAgent.match(/rv:11/),
  37. ieVer=(ie ? ie[1] : (ie11 ? 11 : -1));
  38.  
  39. if (ie && ieVer<10) {
  40. console.log("No blobs on IE ver<10");
  41. return;
  42. }
  43.  
  44. var textFileAsBlob = new Blob([textToWrite], {
  45. type: 'text/plain'
  46. });
  47.  
  48. if (ie || ie11) {
  49. window.navigator.msSaveBlob(textFileAsBlob, fileNameToSaveAs);
  50. } else {
  51. var downloadLink = document.createElement("a");
  52. downloadLink.download = fileNameToSaveAs;
  53. downloadLink.innerHTML = "Download File";
  54.  
  55. if (window.webkitURL !== null) {
  56. // Chrome allows the link to be clicked
  57. // without actually adding it to the DOM.
  58. downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
  59. } else {
  60. // Firefox requires the link to be added to the DOM
  61. // before it can be clicked.
  62. downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
  63. downloadLink.onclick = destroyClickedElement;
  64. downloadLink.style.display = "none";
  65. document.body.appendChild(downloadLink);
  66. }
  67.  
  68. downloadLink.click();
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement