HTML5.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*******************************************************************************
  2. * IBM Confidential
  3. *
  4. * OCO Source Materials
  5. *
  6. * A and PM: PD
  7. *
  8. * (c) Copyright IBM Corp. 2014, 2015
  9. *
  10. * The source code for this program is not published or otherwise divested of
  11. * its trade secrets, irrespective of what has been deposited with the U.S.
  12. * Copyright Office.
  13. ******************************************************************************/
  14. define([
  15. "dojo/_base/declare",
  16. "dojo/_base/lang",
  17. "dojo/_base/array",
  18. "dojox/form/uploader/plugins/HTML5"
  19. ],function(declare, lang, array, dojoHTML5){
  20. var HTML5 = declare("pd/widgets/uploader/HTML5", [dojoHTML5], {
  21. upload: function(data){
  22. var isEdit = data['inSpec'] != null;
  23. this.onBegin(this.getFileList());
  24. this.uploadWithFormData(data, !isEdit);
  25. },
  26. uploadWithFormData: function(data, isGovernorCheck){
  27. //overwrite dojo original one.
  28. var formData = new FormData();
  29. if (data) {
  30. for(var key in data) {
  31. formData.append(key, data[key]);
  32. }
  33. data.uploadType = this.uploadType;
  34. }
  35. if (isGovernorCheck) {
  36. this._filesTMP = this._files;
  37. } else if (this._filesTMP) {
  38. this._files = this._filesTMP;
  39. }
  40. array.some(this._files, function(file){
  41. formData.append(this.name, isGovernorCheck ? '' : file);
  42. return true; //only add the 1st file
  43. }, this);
  44. var xhr = this.createXhr(data, isGovernorCheck);
  45. if (dojo.isSafari && xhr.overrideMimeType) {
  46. xhr.overrideMimeType('text/xml');
  47. }
  48. xhr.send(formData);
  49. },
  50. createXhr: function(data, isGovernorCheck){
  51. //overwrite dojo original one.
  52. var xhr = new XMLHttpRequest();
  53. var timer;
  54. if (!isGovernorCheck) {
  55. xhr.upload.addEventListener("progress", lang.hitch(this, "_xhrProgress"), false);
  56. }
  57. xhr.addEventListener("load", lang.hitch(this, "_xhrProgress"), false);
  58. xhr.addEventListener("error", lang.hitch(this, function(evt){
  59. this.onError(evt);
  60. clearInterval(timer);
  61. }), false);
  62. xhr.addEventListener("abort", lang.hitch(this, function(evt){
  63. this.onAbort(evt);
  64. clearInterval(timer);
  65. }), false);
  66. xhr.onreadystatechange = lang.hitch(this, function(){
  67. if(xhr.readyState === 4){
  68. clearInterval(timer);
  69. try{
  70. var result = "";
  71. if (this.handleAs.toLowerCase() == "json"){
  72. result = JSON.parse(xhr.responseText.replace(/^\{\}&&/,''));
  73. } else if (this.handleAs.toLowerCase() == "xml"){
  74. result = xhr.responseXML;
  75. } else if (this.handleAs.toLowerCase() == "html"){
  76. result = xhr.response;
  77. } else {
  78. result = xhr.responseText;
  79. }
  80. if (xhr.status == 200) {
  81. if (isGovernorCheck) {
  82. var valid = this.onComplete(result, true);
  83. if (valid) {
  84. this.uploadWithFormData(data, false);
  85. }
  86. } else {
  87. this.onComplete(result);
  88. }
  89. } else {
  90. this.onError({
  91. name: xhr.status,
  92. message: xhr.status + " " + xhr.statusText,
  93. response: result //use response as the payload
  94. });
  95. }
  96. } catch(err){
  97. this.onError(err);
  98. }
  99. } else {
  100. console.log(xhr);
  101. }
  102. });
  103. xhr.open("POST", this.getUrl());
  104. xhr.setRequestHeader("Accept","application/" + this.handleAs);
  105. var responseType = "";
  106. if (this.handleAs.toLowerCase() == "json"){
  107. responseType = "json";
  108. } else if (this.handleAs.toLowerCase() == "html"){
  109. responseType = dojo.isSafari?"text":"document";
  110. }
  111. xhr.responseType = responseType;
  112. timer = setInterval(lang.hitch(this, function(){
  113. try{
  114. if(typeof(xhr.statusText)){}
  115. }catch(e){
  116. clearInterval(timer);
  117. }
  118. }),250);
  119. return xhr;
  120. }
  121. });
  122. return HTML5;
  123. });