Base.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.form.uploader.Base"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.form.uploader.Base"] = true;
  8. dojo.provide("dojox.form.uploader.Base");
  9. dojo.require("dijit._Widget");
  10. dojo.require("dijit._Templated");
  11. dojo.declare("dojox.form.uploader.Base", [dijit._Widget, dijit._Templated], {
  12. //
  13. // Version: 1.6
  14. //
  15. // summary:
  16. // The Base class used for dojox.form.Uploader and dojox.form.uploader.FileList.
  17. //
  18. // description:
  19. // Should not be used as a standalone. To be mixed in with other classes.
  20. //
  21. getForm: function(){
  22. // summary:
  23. // Finds the parent form of the Uploader, if it exists.
  24. //
  25. if(!this.form){
  26. var n = this.domNode;
  27. while(n && n.tagName && n !== document.body){
  28. if(n.tagName.toLowerCase() == "form"){
  29. this.form = n;
  30. break;
  31. }
  32. n = n.parentNode;
  33. }
  34. }
  35. return this.form // Node;
  36. },
  37. getUrl: function(){
  38. // summary:
  39. // Finds the URL to upload to, whether it be the action in the parent form, this.url or
  40. // this.uploadUrl
  41. //
  42. if(this.uploadUrl) this.url = this.uploadUrl;
  43. if(this.url) return this.url;
  44. if(this.getForm()) this.url = this.form.action;
  45. return this.url; // String
  46. },
  47. connectForm: function(){
  48. //console.log("connectForm...", this.url, !!this.uploadUrl, !!this.getForm())
  49. this.url = this.getUrl();
  50. if(!this._fcon && !!this.getForm()){
  51. this._fcon = true;
  52. this.connect(this.form, "onsubmit", function(evt){
  53. dojo.stopEvent(evt);
  54. this.submit(dojo.formToObject(this.form));
  55. });
  56. //console.log("----------------form connected:", this.url)
  57. }
  58. //console.log("form:", this.form, this.url);
  59. },
  60. supports: function(what){
  61. // summary:
  62. // Does feature testing for uploader capabilities. (No browser sniffing - yay)
  63. //
  64. if(!this._hascache){
  65. this._hascache = {
  66. testDiv: dojo.create("div"),
  67. testInput: dojo.create("input", {type:"file"}),
  68. xhr:!!window.XMLHttpRequest ? new XMLHttpRequest() : {}
  69. };
  70. dojo.style(this._hascache.testDiv, "opacity", .7);
  71. }
  72. switch(what){
  73. case "FormData":
  74. return !!window.FormData;
  75. case "sendAsBinary":
  76. return !!this._hascache.xhr.sendAsBinary;
  77. case "opacity":
  78. return dojo.style(this._hascache.testDiv, "opacity") == .7;
  79. case "multiple":
  80. if(this.force == "flash" || this.force == "iframe") return false;
  81. var res = dojo.attr(this._hascache.testInput, "multiple");
  82. return res===true || res===false; // IE will be undefined
  83. }
  84. return false; // Boolean
  85. },
  86. getMimeType: function(){
  87. // summary:
  88. // Returns the mime type that should be used in an HTML5 upload form. Return result
  89. // may change as the current use is very generic.
  90. //
  91. return "application/octet-stream"; //image/gif
  92. },
  93. getFileType: function(/* String */name){
  94. // summary:
  95. // Gets the extension of a file
  96. return name.substring(name.lastIndexOf(".")+1).toUpperCase(); // String
  97. },
  98. convertBytes: function(bytes){
  99. // summary:
  100. // Converts bytes. Returns an object with all conversions. The "value" property is
  101. // considered the most likely desired result.
  102. //
  103. var kb = Math.round(bytes/1024*100000)/100000;
  104. var mb = Math.round(bytes/1048576*100000)/100000;
  105. var gb = Math.round(bytes/1073741824*100000)/100000;
  106. var value = bytes;
  107. if(kb>1) value = kb.toFixed(1)+" kb";
  108. if(mb>1) value = mb.toFixed(1)+" mb";
  109. if(gb>1) value = gb.toFixed(1)+" gb";
  110. return {
  111. kb:kb,
  112. mb:mb,
  113. gb:gb,
  114. bytes:bytes,
  115. value: value
  116. }; // Object
  117. }
  118. });
  119. }