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