1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
//>>built
define("dojox/io/xhrMultiPart", [
"dojo/_base/kernel",
"dojo/_base/array",
"dojo/_base/xhr",
"dojo/query",
"dojox/uuid/generateRandomUuid"
], function(dojo, array, xhr, query, generateRandomUuid){
dojo.getObject("io.xhrMultiPart", true, dojox);
/*=====
dojox.io.__xhrContentArgs = function(){
// name: String
// Name of the form value.
// content: String
// The contents of the value.
// filename: String?
// An optional filename to pass to the server, as defined by the boundary.
// contentType: String?
// An optional content-type (MIME) to pass to the server, if value is being
// treated as a file.
// charset: String?
// Optional charset to pass, for the server to interpret the file correctly.
// contentTransferEncoding: String?
// Optional transfer encoding header value.
this.name = name;
this.content = content;
this.filename = filename;
this.contentType = contentType;
this.charset = charset;
this.contentTransferEncoding = contentTransferEncoding;
}
=====*/
function _createPart(/* dojox.io.__xhrContentArgs */args, /* String */boundary){
// summary
// Assemble an array of boundary parts based on the passed values in args.
if(!args["name"] && !args["content"]){
throw new Error("Each part of a multi-part request requires 'name' and 'content'.");
}
var tmp = [];
tmp.push(
"--" + boundary,
"Content-Disposition: form-data; name=\"" + args.name + "\"" + (args["filename"] ? "; filename=\"" + args.filename + "\"" : "")
);
if(args["contentType"]){
var ct = "Content-Type: " + args.contentType;
if(args["charset"]){
ct += "; Charset=" + args.charset;
}
tmp.push(ct);
}
if(args["contentTransferEncoding"]){
tmp.push("Content-Transfer-Encoding: " + args.contentTransferEncoding);
}
tmp.push("", args.content);
return tmp; // Array
}
function _partsFromNode(/* DOMNode */node, /* String */boundary){
// summary
// Assemble an array of boundary parts based on the passed FORM node.
var o=dojo.formToObject(node), parts=[];
for(var p in o){
if(dojo.isArray(o[p])){
dojo.forEach(o[p], function(item){
parts = parts.concat(_createPart({ name: p, content: item }, boundary));
});
} else {
parts = parts.concat(_createPart({ name: p, content: o[p] }, boundary));
}
}
return parts; // Array
}
/*=====
dojox.io.__xhrMultiArgs = function(){
// url: String
// URL to server endpoint.
// content: Object?
// Contains properties with string values. These
// properties will be serialized using multi-part
// boundaries.
// file: Object?
// Alias for "content". Provided for backwards compatibility.
// timeout: Integer?
// Milliseconds to wait for the response. If this time
// passes, the then error callbacks are called.
// form: DOMNode?
// DOM node for a form. Used to extract the form values
// and send to the server; each form value will be serialized
// using multi-part boundaries.
// preventCache: Boolean?
// Default is false. If true, then a
// "dojo.preventCache" parameter is sent in the request
// with a value that changes with each request
// (timestamp). Useful only with GET-type requests.
// handleAs: String?
// Acceptable values depend on the type of IO
// transport (see specific IO calls for more information).
// load: Function?
// function(response, ioArgs){}. response is an Object, ioArgs
// is of type dojo.__IoCallbackArgs. The load function will be
// called on a successful response.
// error: Function?
// function(response, ioArgs){}. response is an Object, ioArgs
// is of type dojo.__IoCallbackArgs. The error function will
// be called in an error case.
// handle: Function?
// function(response, ioArgs){}. response is an Object, ioArgs
// is of type dojo.__IoCallbackArgs. The handle function will
// be called in either the successful or error case.
this.url = url;
this.content = content;
this.file = file;
this.timeout = timeout;
this.form = form;
this.preventCache = preventCache;
this.handleAs = handleAs;
this.load = load;
this.error = error;
this.handle = handle;
}
=====*/
dojox.io.xhrMultiPart = function(/* dojox.io.__xhrMultiArgs */args){
if(!args["file"] && !args["content"] && !args["form"]){
throw new Error("content, file or form must be provided to dojox.io.xhrMultiPart's arguments");
}
// unique guid as a boundary value for multipart posts
var boundary=generateRandomUuid(), tmp=[], out="";
if(args["file"] || args["content"]){
var v = args["file"] || args["content"];
dojo.forEach((dojo.isArray(v) ? v : [v]), function(item){
tmp = tmp.concat(_createPart(item, boundary));
});
}
else if(args["form"]){
if(query("input[type=file]", args["form"]).length){
throw new Error("dojox.io.xhrMultiPart cannot post files that are values of an INPUT TYPE=FILE. Use dojo.io.iframe.send() instead.");
}
tmp = _partsFromNode(args["form"], boundary);
}
if(tmp.length){
tmp.push("--"+boundary+"--", "");
out = tmp.join("\r\n");
}
console.log(out);
return dojo.rawXhrPost(dojo.mixin(args, {
contentType: "multipart/form-data; boundary=" + boundary,
postData: out
})); // dojo.Deferred
};
return dojox.io.xhrMultiPart;
});
|