Changeset 13839

Show
Ignore:
Timestamp:
05/30/08 10:10:52 (6 months ago)
Author:
ttrenka
Message:

Fixes #5798. Adds the ability to submit a form, prevents inclusion of INPUT TYPE=FILE, added documentation and tests, and set up an alias for the original "file" argument so that it also uses "content". !strict.

Location:
dojox/trunk/io
Files:
3 added
1 modified

Legend:

Unmodified
Added
Removed
  • dojox/trunk/io/xhrMultiPart.js

    r11921 r13839  
    11dojo.provide("dojox.io.xhrMultiPart"); 
    2  
    3 dojo.require("dojo._base.xhr"); 
    42dojo.require("dojox.uuid.generateRandomUuid"); 
    53 
    64(function(){ 
    7         function _createPart(args, boundary){ 
     5        /*===== 
     6        dojox.io.__xhrContentArgs = function(){ 
     7                //      name: String 
     8                //              Name of the form value. 
     9                //      content: String 
     10                //              The contents of the value. 
     11                //      filename: String? 
     12                //              An optional filename to pass to the server, as defined by the boundary. 
     13                //      contentType: String? 
     14                //              An optional content-type (MIME) to pass to the server, if value is being 
     15                //              treated as a file. 
     16                //      charset: String? 
     17                //              Optional charset to pass, for the server to interpret the file correctly. 
     18                //      contentTransferEncoding: String? 
     19                //              Optional transfer encoding header value. 
     20                this.name = name; 
     21                this.content = content; 
     22                this.filename = filename; 
     23                this.contentType = contentType; 
     24                this.charset = charset; 
     25                this.contentTransferEncoding = contentTransferEncoding; 
     26        } 
     27        =====*/ 
     28        function _createPart(/* dojox.io.__xhrContentArgs */args, /* String */boundary){ 
     29                //      summary 
     30                //              Assemble an array of boundary parts based on the passed values in args. 
    831                if(!args["name"] && !args["content"]){ 
    932                        throw new Error("Each part of a multi-part request requires 'name' and 'content'."); 
     
    1134 
    1235                var tmp = []; 
    13                 tmp.push("--" + boundary, 
    14                                  "Content-Disposition: form-data; name=\"" + args.name + "\"" + 
    15                                  (args["filename"] ? "; filename=\"" + args.filename + "\"" : "")); 
     36                tmp.push( 
     37                        "--" + boundary, 
     38                         "Content-Disposition: form-data; name=\"" + args.name + "\"" + (args["filename"] ? "; filename=\"" + args.filename + "\"" : "") 
     39                ); 
    1640 
    1741                if(args["contentType"]){ 
     
    2650                        tmp.push("Content-Transfer-Encoding: " + args.contentTransferEncoding); 
    2751                } 
    28  
    2952                tmp.push("", args.content); 
    30  
    31                 return tmp; 
     53                return tmp;             //      Array 
    3254        } 
    3355 
    34         function _needIframe(node){ 
    35                 return (!!(dojo.query("input[type=file]", node).length)); 
     56        function _partsFromNode(/* DOMNode */node, /* String */boundary){ 
     57                //      summary 
     58                //              Assemble an array of boundary parts based on the passed FORM node. 
     59                var o=dojo.formToObject(node), parts=[]; 
     60                for(var p in o){ 
     61                        if(dojo.isArray(o[p])){ 
     62                                dojo.forEach(o[p], function(item){ 
     63                                        parts = parts.concat(_createPart({ name: p, content: item }, boundary)); 
     64                                }); 
     65                        } else { 
     66                                parts = parts.concat(_createPart({ name: p, content: o[p] }, boundary)); 
     67                        } 
     68                } 
     69                return parts;   //      Array 
    3670        } 
    3771 
    38         function _partsFromNode(node, boundary){ 
    39                 // TODO: write this function! 
    40                 var tmp = []; 
    41                 return tmp; 
     72        /*===== 
     73        dojox.io.__xhrMultiArgs = function(){ 
     74                //      url: String 
     75                //              URL to server endpoint. 
     76                //      content: Object? 
     77                //              Contains properties with string values. These 
     78                //              properties will be serialized using multi-part 
     79                //              boundaries. 
     80                //      file: Object? 
     81                //              Alias for "content".  Provided for backwards compatibility. 
     82                //      timeout: Integer? 
     83                //              Milliseconds to wait for the response. If this time 
     84                //              passes, the then error callbacks are called. 
     85                //      form: DOMNode? 
     86                //              DOM node for a form. Used to extract the form values 
     87                //              and send to the server; each form value will be serialized 
     88                //              using multi-part boundaries. 
     89                //      preventCache: Boolean? 
     90                //              Default is false. If true, then a 
     91                //              "dojo.preventCache" parameter is sent in the request 
     92                //              with a value that changes with each request 
     93                //              (timestamp). Useful only with GET-type requests. 
     94                //      handleAs: String? 
     95                //              Acceptable values depend on the type of IO 
     96                //              transport (see specific IO calls for more information). 
     97                //      load: Function? 
     98                //              function(response, ioArgs){}. response is an Object, ioArgs 
     99                //              is of type dojo.__IoCallbackArgs. The load function will be 
     100                //              called on a successful response. 
     101                //      error: Function? 
     102                //              function(response, ioArgs){}. response is an Object, ioArgs 
     103                //              is of type dojo.__IoCallbackArgs. The error function will 
     104                //              be called in an error case.  
     105                //      handle: Function? 
     106                //              function(response, ioArgs){}. response is an Object, ioArgs 
     107                //              is of type dojo.__IoCallbackArgs. The handle function will 
     108                //              be called in either the successful or error case. 
     109                this.url = url; 
     110                this.content = content; 
     111                this.file = file; 
     112                this.timeout = timeout; 
     113                this.form = form; 
     114                this.preventCache = preventCache; 
     115                this.handleAs = handleAs; 
     116                this.load = load; 
     117                this.error = error; 
     118                this.handle = handle; 
    42119        } 
    43  
    44         dojox.io.xhrMultiPart = function(args){ 
    45                 if(!args["file"] && !args["form"]){ 
    46                         throw new Error("file or form must be provided to dojox.io.xhrMultiPart's arguments"); 
     120        =====*/ 
     121        dojox.io.xhrMultiPart = function(/* dojox.io.__xhrMultiArgs */args){ 
     122                if(!args["file"] && !args["content"] && !args["form"]){ 
     123                        throw new Error("content, file or form must be provided to dojox.io.xhrMultiPart's arguments"); 
    47124                } 
    48125 
    49126                // unique guid as a boundary value for multipart posts 
    50                 var boundary = dojox.uuid.generateRandomUuid(); 
    51  
    52                 var tmp = []; 
    53                 var out = ""; 
    54  
    55                 if(args["file"]){ 
    56                         var d = (dojo.isArray(args.file) ? args.file : [args.file]); 
    57  
    58                         for(var i=0; i < d.length; i++){ 
    59                                 tmp = tmp.concat(_createPart(d[i], boundary)); 
     127                var boundary=dojox.uuid.generateRandomUuid(), tmp=[], out=""; 
     128                if(args["file"] || args["content"]){ 
     129                        var v = args["file"] || args["content"]; 
     130                        dojo.forEach((dojo.isArray(v) ? v : [v]), function(item){ 
     131                                tmp = tmp.concat(_createPart(item, boundary)); 
     132                        }); 
     133                } 
     134                else if(args["form"]){ 
     135                        if(dojo.query("input[type=file]", args["form"]).length){ 
     136                                throw new Error("dojox.io.xhrMultiPart cannot post files that are values of an INPUT TYPE=FILE.  Use dojo.io.iframe.send() instead."); 
    60137                        } 
    61                 } 
    62  
    63                 if(args["form"]){ 
    64                         tmp = tmp.concat(_partsFromNode(args["form"], boundary)); 
     138                        tmp = _partsFromNode(args["form"], boundary); 
    65139                } 
    66140 
     
    70144                } 
    71145 
     146                console.log(out); 
     147 
    72148                return dojo.rawXhrPost(dojo.mixin(args, { 
    73149                        contentType: "multipart/form-data; boundary=" + boundary, 
    74150                        postData: out 
    75                 })); 
     151                }));    //      dojo.Deferred 
    76152        } 
    77153})();