jszip-utils.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*!
  2. JSZipUtils - A collection of cross-browser utilities to go along with JSZip.
  3. <http://stuk.github.io/jszip-utils>
  4. (c) 2014 Stuart Knightley, David Duponchel
  5. Dual licenced under the MIT license or GPLv3. See https://raw.github.com/Stuk/jszip-utils/master/LICENSE.markdown.
  6. */
  7. !function(e){"object"==typeof exports?module.exports=e():"function"==typeof define&&define.amd?define(e):"undefined"!=typeof window?window.JSZipUtils=e():"undefined"!=typeof global?global.JSZipUtils=e():"undefined"!=typeof self&&(self.JSZipUtils=e())}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  8. 'use strict';
  9. var JSZipUtils = {};
  10. // just use the responseText with xhr1, response with xhr2.
  11. // The transformation doesn't throw away high-order byte (with responseText)
  12. // because JSZip handles that case. If not used with JSZip, you may need to
  13. // do it, see https://developer.mozilla.org/En/Using_XMLHttpRequest#Handling_binary_data
  14. JSZipUtils._getBinaryFromXHR = function (xhr) {
  15. // for xhr.responseText, the 0xFF mask is applied by JSZip
  16. return xhr.response || xhr.responseText;
  17. };
  18. // taken from jQuery
  19. function createStandardXHR() {
  20. try {
  21. return new window.XMLHttpRequest();
  22. } catch( e ) {}
  23. }
  24. function createActiveXHR() {
  25. try {
  26. return new window.ActiveXObject("Microsoft.XMLHTTP");
  27. } catch( e ) {}
  28. }
  29. // Create the request object
  30. var createXHR = window.ActiveXObject ?
  31. /* Microsoft failed to properly
  32. * implement the XMLHttpRequest in IE7 (can't request local files),
  33. * so we use the ActiveXObject when it is available
  34. * Additionally XMLHttpRequest can be disabled in IE7/IE8 so
  35. * we need a fallback.
  36. */
  37. function() {
  38. return createStandardXHR() || createActiveXHR();
  39. } :
  40. // For all other browsers, use the standard XMLHttpRequest object
  41. createStandardXHR;
  42. JSZipUtils.getBinaryContent = function(path, callback) {
  43. /*
  44. * Here is the tricky part : getting the data.
  45. * In firefox/chrome/opera/... setting the mimeType to 'text/plain; charset=x-user-defined'
  46. * is enough, the result is in the standard xhr.responseText.
  47. * cf https://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequest#Receiving_binary_data_in_older_browsers
  48. * In IE <= 9, we must use (the IE only) attribute responseBody
  49. * (for binary data, its content is different from responseText).
  50. * In IE 10, the 'charset=x-user-defined' trick doesn't work, only the
  51. * responseType will work :
  52. * http://msdn.microsoft.com/en-us/library/ie/hh673569%28v=vs.85%29.aspx#Binary_Object_upload_and_download
  53. *
  54. * I'd like to use jQuery to avoid this XHR madness, but it doesn't support
  55. * the responseType attribute : http://bugs.jquery.com/ticket/11461
  56. */
  57. try {
  58. var xhr = createXHR();
  59. xhr.open('GET', path, true);
  60. // recent browsers
  61. if ("responseType" in xhr) {
  62. xhr.responseType = "arraybuffer";
  63. }
  64. // older browser
  65. if(xhr.overrideMimeType) {
  66. xhr.overrideMimeType("text/plain; charset=x-user-defined");
  67. }
  68. xhr.onreadystatechange = function(evt) {
  69. var file, err;
  70. // use `xhr` and not `this`... thanks IE
  71. if (xhr.readyState === 4) {
  72. if (xhr.status === 200 || xhr.status === 0) {
  73. file = null;
  74. err = null;
  75. try {
  76. file = JSZipUtils._getBinaryFromXHR(xhr);
  77. } catch(e) {
  78. err = new Error(e);
  79. }
  80. callback(err, file);
  81. } else {
  82. callback(new Error("Ajax error for " + path + " : " + this.status + " " + this.statusText), null);
  83. }
  84. }
  85. };
  86. xhr.send();
  87. } catch (e) {
  88. callback(new Error(e), null);
  89. }
  90. };
  91. // export
  92. module.exports = JSZipUtils;
  93. // enforcing Stuk's coding style
  94. // vim: set shiftwidth=4 softtabstop=4:
  95. },{}]},{},[1])
  96. (1)
  97. });
  98. ;