| Total Complexity | 9 |
| Complexity/F | 3 |
| Lines of Code | 55 |
| Function Count | 3 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | /** |
||
| 7 | (function(self, dom, console) {
|
||
| 8 | /** |
||
| 9 | * @param {object} oRequest A request object, created initially by a call to <jaxon.ajax.request.initialize>
|
||
| 10 | * @param {string=} oRequest.upload The HTML file upload field id
|
||
| 11 | * |
||
| 12 | * @returns {boolean}
|
||
| 13 | */ |
||
| 14 | const initRequest = (oRequest) => {
|
||
| 15 | if (!oRequest.upload) {
|
||
| 16 | return false; |
||
| 17 | } |
||
| 18 | |||
| 19 | oRequest.upload = {
|
||
| 20 | id: oRequest.upload, |
||
| 21 | input: null, |
||
| 22 | form: null, |
||
| 23 | }; |
||
| 24 | const input = dom.$(oRequest.upload.id); |
||
| 25 | |||
| 26 | if (!input) {
|
||
| 27 | console.log('Unable to find input field for file upload with id ' + oRequest.upload.id);
|
||
|
|
|||
| 28 | return false; |
||
| 29 | } |
||
| 30 | if (input.type !== 'file') {
|
||
| 31 | console.log('The upload input field with id ' + oRequest.upload.id + ' is not of type file');
|
||
| 32 | return false; |
||
| 33 | } |
||
| 34 | if (input.files.length === 0) {
|
||
| 35 | console.log('There is no file selected for upload in input field with id ' + oRequest.upload.id);
|
||
| 36 | return false; |
||
| 37 | } |
||
| 38 | if (input.name === undefined) {
|
||
| 39 | console.log('The upload input field with id ' + oRequest.upload.id + ' has no name attribute');
|
||
| 40 | return false; |
||
| 41 | } |
||
| 42 | oRequest.upload.input = input; |
||
| 43 | oRequest.upload.form = input.form; |
||
| 44 | return true; |
||
| 45 | }; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Check upload data and initialize the request. |
||
| 49 | * |
||
| 50 | * @param {object} oRequest A request object, created initially by a call to <jaxon.ajax.request.initialize>
|
||
| 51 | * |
||
| 52 | * @returns {void}
|
||
| 53 | */ |
||
| 54 | self.initialize = (oRequest) => {
|
||
| 55 | // The content type shall not be set when uploading a file with FormData. |
||
| 56 | // It will be set by the browser. |
||
| 57 | if (!initRequest(oRequest)) {
|
||
| 58 | oRequest.postHeaders['content-type'] = oRequest.contentType; |
||
| 59 | } |
||
| 60 | } |
||
| 61 | })(jaxon.ajax.upload, jaxon.utils.dom, console); |
||
| 62 |