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