| 1 |
define([ |
| 2 |
"dojo/_base/kernel", |
| 3 |
"dojo/_base/array", |
| 4 |
"dojo/_base/xhr", |
| 5 |
"dojo/query", |
| 6 |
"dojox/uuid/generateRandomUuid" |
| 7 |
], function(dojo, array, xhr, query, generateRandomUuid){ |
| 8 |
dojo.getObject("io.xhrMultiPart", true, dojox); |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
function _createPart(args, boundary){ |
| 28 |
|
| 29 |
|
| 30 |
if(!args["name"] && !args["content"]){ |
| 31 |
throw new Error("Each part of a multi-part request requires 'name' and 'content'."); |
| 32 |
} |
| 33 |
|
| 34 |
var tmp = []; |
| 35 |
tmp.push( |
| 36 |
"--" + boundary, |
| 37 |
"Content-Disposition: form-data; name=\"" + args.name + "\"" + (args["filename"] ? "; filename=\"" + args.filename + "\"" : "") |
| 38 |
); |
| 39 |
|
| 40 |
if(args["contentType"]){ |
| 41 |
var ct = "Content-Type: " + args.contentType; |
| 42 |
if(args["charset"]){ |
| 43 |
ct += "; Charset=" + args.charset; |
| 44 |
} |
| 45 |
tmp.push(ct); |
| 46 |
} |
| 47 |
|
| 48 |
if(args["contentTransferEncoding"]){ |
| 49 |
tmp.push("Content-Transfer-Encoding: " + args.contentTransferEncoding); |
| 50 |
} |
| 51 |
tmp.push("", args.content); |
| 52 |
return tmp; |
| 53 |
} |
| 54 |
|
| 55 |
function _partsFromNode(node, boundary){ |
| 56 |
|
| 57 |
|
| 58 |
var o=dojo.formToObject(node), parts=[]; |
| 59 |
for(var p in o){ |
| 60 |
if(dojo.isArray(o[p])){ |
| 61 |
dojo.forEach(o[p], function(item){ |
| 62 |
parts = parts.concat(_createPart({ name: p, content: item }, boundary)); |
| 63 |
}); |
| 64 |
} else { |
| 65 |
parts = parts.concat(_createPart({ name: p, content: o[p] }, boundary)); |
| 66 |
} |
| 67 |
} |
| 68 |
return parts; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
dojox.io.xhrMultiPart = function(args){ |
| 111 |
if(!args["file"] && !args["content"] && !args["form"]){ |
| 112 |
throw new Error("content, file or form must be provided to dojox.io.xhrMultiPart's arguments"); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
var boundary=generateRandomUuid(), tmp=[], out=""; |
| 117 |
if(args["file"] || args["content"]){ |
| 118 |
var v = args["file"] || args["content"]; |
| 119 |
dojo.forEach((dojo.isArray(v) ? v : [v]), function(item){ |
| 120 |
tmp = tmp.concat(_createPart(item, boundary)); |
| 121 |
}); |
| 122 |
} |
| 123 |
else if(args["form"]){ |
| 124 |
if(query("input[type=file]", args["form"]).length){ |
| 125 |
throw new Error("dojox.io.xhrMultiPart cannot post files that are values of an INPUT TYPE=FILE. Use dojo.io.iframe.send() instead."); |
| 126 |
} |
| 127 |
tmp = _partsFromNode(args["form"], boundary); |
| 128 |
} |
| 129 |
|
| 130 |
if(tmp.length){ |
| 131 |
tmp.push("--"+boundary+"--", ""); |
| 132 |
out = tmp.join("\r\n"); |
| 133 |
} |
| 134 |
|
| 135 |
console.log(out); |
| 136 |
|
| 137 |
return dojo.rawXhrPost(dojo.mixin(args, { |
| 138 |
contentType: "multipart/form-data; boundary=" + boundary, |
| 139 |
postData: out |
| 140 |
})); |
| 141 |
}; |
| 142 |
|
| 143 |
return dojox.io.xhrMultiPart; |
| 144 |
}); |