xhrMultiPart.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. if (!dojo._hasResource["dojox.io.xhrMultiPart"]) { // _hasResource checks added
  2. // by build. Do not use
  3. // _hasResource directly in
  4. // your code.
  5. dojo._hasResource["dojox.io.xhrMultiPart"] = true;
  6. dojo.provide("dojox.io.xhrMultiPart");
  7. dojo.require("dojo._base.xhr");
  8. dojo.require("dojox.uuid.generateRandomUuid");
  9. (function() {
  10. function _createPart(args, boundary) {
  11. if (!args["name"] && !args["content"]) {
  12. throw new Error("Each part of a multi-part request requires 'name' and 'content'.");
  13. }
  14. var tmp = [];
  15. tmp.push("--" + boundary, "Content-Disposition: form-data; name=\""
  16. + args.name
  17. + "\""
  18. + (args["filename"] ? "; filename=\""
  19. + args.filename + "\"" : ""));
  20. if (args["contentType"]) {
  21. var ct = "Content-Type: " + args.contentType;
  22. if (args["charset"]) {
  23. ct += "; Charset=" + args.charset;
  24. }
  25. tmp.push(ct);
  26. }
  27. if (args["contentTransferEncoding"]) {
  28. tmp.push("Content-Transfer-Encoding: "
  29. + args.contentTransferEncoding);
  30. }
  31. tmp.push("", args.content);
  32. return tmp;
  33. }
  34. function _needIframe(node) {
  35. return (!!(dojo.query("input[type=file]", node).length));
  36. }
  37. function _partsFromNode(node, boundary) {
  38. // TODO: write this function!
  39. var tmp = [];
  40. return tmp;
  41. }
  42. dojox.io.xhrMultiPart = function(args) {
  43. if (!args["file"] && !args["formNode"]) {
  44. throw new Error("file or formNode must be provided to dojox.io.xhrMultiPart's arguments");
  45. }
  46. // unique guid as a boundary value for multipart posts
  47. var boundary = dojox.uuid.generateRandomUuid();
  48. var tmp = [];
  49. var out = "";
  50. if (args["file"]) {
  51. var d = (dojo.isArray(args.file) ? args.file : [args.file]);
  52. for (var i = 0; i < d.length; i++) {
  53. tmp = tmp.concat(_createPart(d[i], boundary));
  54. }
  55. }
  56. if (args["formNode"]) {
  57. tmp = tmp.concat(_partsFromNode(args["formNode"], boundary));
  58. }
  59. if (tmp.length) {
  60. tmp.push("--" + boundary + "--", "");
  61. out = tmp.join("\r\n");
  62. }
  63. return dojo.rawXhrPost(dojo.mixin(args, {
  64. contentType : "multipart/form-data; boundary="
  65. + boundary,
  66. postData : out
  67. }));
  68. }
  69. })();
  70. }