aboutsummaryrefslogtreecommitdiff
path: root/includes/js/dojox/io/xhrMultiPart.js
blob: 57922679682fb87e3b24707d3a7fc4b3cfb3716e (plain)
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
if(!dojo._hasResource["dojox.io.xhrMultiPart"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.io.xhrMultiPart"] = true;
dojo.provide("dojox.io.xhrMultiPart");

dojo.require("dojo._base.xhr");
dojo.require("dojox.uuid.generateRandomUuid");

(function(){
	function _createPart(args, boundary){
		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;
	}

	function _needIframe(node){
		return (!!(dojo.query("input[type=file]", node).length));
	}

	function _partsFromNode(node, boundary){
		// TODO: write this function!
		var tmp = [];
		return tmp;
	}

	dojox.io.xhrMultiPart = function(args){
		if(!args["file"] && !args["form"]){
			throw new Error("file or form must be provided to dojox.io.xhrMultiPart's arguments");
		}

		// unique guid as a boundary value for multipart posts
		var boundary = dojox.uuid.generateRandomUuid();

		var tmp = [];
		var out = "";

		if(args["file"]){
			var d = (dojo.isArray(args.file) ? args.file : [args.file]);

			for(var i=0; i < d.length; i++){
				tmp = tmp.concat(_createPart(d[i], boundary));
			}
		}

		if(args["form"]){
			tmp = tmp.concat(_partsFromNode(args["form"], boundary));
		}

		if(tmp.length){
			tmp.push("--"+boundary+"--", "");
			out = tmp.join("\r\n");
		}

		return dojo.rawXhrPost(dojo.mixin(args, {
			contentType: "multipart/form-data; boundary=" + boundary,
			postData: out
		}));
	}
})();

}