1 | /** |
||
2 | * Class: jaxon.ajax.upload |
||
3 | * |
||
4 | * global: jaxon |
||
5 | */ |
||
6 | |||
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); |
||
0 ignored issues
–
show
Debugging Code
introduced
by
![]() |
|||
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 |