-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathandroidFileUploadFix-1.1.0.js
231 lines (183 loc) · 7.6 KB
/
androidFileUploadFix-1.1.0.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
Android File Upload Fix ver. 1.1.0 (Agate Java Script Plugin)
(Fixes the issue that <input type="file"> does not work on Android 4.4.2. )
Last modified on 2014.11.07
Created by ApplusForm.com on 2014.10.30
Permission is granted to copy, distribute, modify under the terms of ApplusForm License.
Copyright (C) 2014 ApplusForm.com. All rights reserved.
*/
var fixAFU_buttonInnerHTML = "Choose File";
var fixAFU_fileNameInnerHTML = "No file chosen";
var fixAFU_uploadStartMessage = "Please wait...";
var fixAFU_chooseFileForUploadMessage = "Choose file for upload";
function fixAFU_replaceFormSubmit(form) {
if (form.submit != fixAFU_submit) {
form.submit = fixAFU_submit;
if (form.onsubmit == null) {
form.onsubmit = function (e) {
this.submit();
e.preventDefault();
return false;
};
} else if (form.onsubmit != null) {
form.__oldOnsubmit = form.onsubmit;
form.onsubmit = function (e) {
var ret = form.__oldOnsubmit(e);
if (ret != false && !e.defaultPrevented) {
this.submit();
}
e.preventDefault();
return false;
};
}
}
}
function fixAFU_escapeFunctionScript(text) {
text = text.replace(/\\/gm, "\\\\");
text = text.replace(/\n/gm, "\\n");
text = text.replace(/\r/gm, "\\r");
text = text.replace(/'/gm, "\\'");
return text;
}
function fixAFU_onCompleteFileUpload(args) {
if (args.responseCode == 200) {
//document.getElementsByTagName("html")[0].innerHTML = args.responseValue;
agate.runScript("caller.setHtml('" + fixAFU_escapeFunctionScript(args.responseValue) + "', '" + fixAFU_lastAction + "')");
} else {
alert("error: " + args.responseCode + "\n" + args.errorMessage + "\n" + args.responseValue);
}
}
function fixAFU_submit() {
try {
var formTags = new Array();
var i;
{
var inputTags = this.getElementsByTagName("input");
var selectTags = this.getElementsByTagName("select");
var textAreaTags = this.getElementsByTagName("textarea");
for (i = 0; i < inputTags.length; ++i)
formTags.push(inputTags[i]);
for (i = 0; i < selectTags.length; ++i)
formTags.push(selectTags[i]);
for (i = 0; i < textAreaTags.length; ++i)
formTags.push(textAreaTags[i]);
}
agate.http.addEventListener('onComplete', 'fixAFU_onCompleteFileUpload')
var uploadRequestId = agate.http.createRequestId();
agate.http.clearPostFileInfo(uploadRequestId);
var param = "";
var count = 0;
for (i = 0; i < formTags.length; ++i) {
var inputTag = formTags[i];
if (inputTag.type == "file") {
var infoIndex = inputTag.getAttribute("inputInfoIndex");
if (typeof (infoIndex) == "undefined")
continue;
var inputInfo = window.fixAFU_inputInfos[infoIndex];
if (typeof (inputInfo.choosenFilePath) == "undefined")
inputInfo.choosenFilePath = "";
agate.http.addPostFileInfo(uploadRequestId, inputInfo.inputTag.name, inputInfo.choosenFilePath);
count++;
} else {
param = param + inputTag.name + "=" + inputTag.value + "|";
}
}
param = param + "_charset_=UTF-8" + "|";
//alert("file count : " + count);
window.fixAFU_lastAction = this.action;
agate.device.toastPopup(fixAFU_uploadStartMessage);
agate.http.post(uploadRequestId, this.action, param);
} catch (e) {
alert(e);
}
}
function getDataUrlFromAgatePath(path) {
var ext = path.split('.').pop();
var dataUrl = "data:";
if (ext.lastIndexOf('/') < 0) {
ext = ext.toLowerCase();
if (ext == "png" || ext == "jpg") {
dataUrl += "image/" + ext;
}
}
dataUrl += ";base64," + agate.base64.read(path);
return dataUrl;
}
// map <INPUT type="file" id="file1" <IMG id="img1"
function getImgElementForInputTag(inputTag) {
// imgRecipe -> imgRecipeIMG
if (inputTag.id == "file1")
return document.getElementById("img1");
if (inputTag.id == "file2")
return document.getElementById("img2");
return null;
}
function fixAFU_onFileChoose(args) {
try {
if (typeof (args.path) == "undefined" || args.path == null || args.path == "")
return;
agate.appLauncher.removeEventListener("onComplete", "fixAFU_onFileChoose");
//var choosenFilePath = agate.file.pathToUrl(args.path);
var choosenFilePath = args.path;
var inputInfo = window.fixAFU_inputInfos[window.fixAFU_lastInputInfo];
var fileName = choosenFilePath.substring(choosenFilePath.lastIndexOf('/') + 1);
inputInfo.patchedTitle.innerHTML = fileName;
inputInfo.choosenFilePath = choosenFilePath;
// image preview
var img = getImgElementForInputTag(inputInfo.inputTag);
if (img != null)
img.setAttribute("src", getDataUrlFromAgatePath(choosenFilePath));
fixAFU_replaceFormSubmit(inputInfo.inputTag.form);
} catch (e) {
alert(e);
}
};
function fixAndroidFileUpload() {
var version = agate.device.os.version();
if (version != "4.4" && version != "4.4.1" && version != "4.4.2")
return;
if (agate.device.os.platform() != "Android")
return;
if (typeof(window.fixAFU_inputInfos) == "undefined")
window.fixAFU_inputInfos = new Array();
var inputTags = document.getElementsByTagName("input");
var i;
for (i = 0; i < inputTags.length; ++i) {
var inputTag = inputTags[i];
if (inputTag.type == "file" && inputTag.style.display != "none") {
if (inputTag.hasAttribute("inputInfoIndex"))
continue;
var infoIndex = window.fixAFU_inputInfos.length;
inputTag.setAttribute("inputInfoIndex", infoIndex);
inputTag.style.display = "none";
var patchedButton = document.createElement("button");
patchedButton.setAttribute("inputInfoIndex", infoIndex);
patchedButton.type = "button";
patchedButton.innerHTML = fixAFU_buttonInnerHTML;
inputTag.parentNode.insertBefore(patchedButton, inputTag);
patchedButton.form = inputTag.form;
var patchedTitle = document.createElement("span");
patchedTitle.setAttribute("inputInfoIndex", infoIndex);
patchedTitle.innerHTML = fixAFU_fileNameInnerHTML;
inputTag.parentNode.insertBefore(patchedTitle, inputTag);
patchedButton.onclick = function (e) {
window.fixAFU_lastInputInfo = e.target.getAttribute("inputInfoIndex");
agate.appLauncher.setSaveForImageCapture(true);
agate.appLauncher.setSaveDirForImageCapture('storage:Photo/');
agate.appLauncher.addEventListener("onComplete", "fixAFU_onFileChoose");
agate.runScript("device.systemPopup('', '" + fixAFU_chooseFileForUploadMessage + "','Camera:appLauncher.exec(\\'imageCapture\\')','Gallery:appLauncher.exec(\\'albums\\')','Cancel')");
};
var inputInfo = new Object();
window.fixAFU_inputInfos.push(inputInfo);
inputInfo.inputTag = inputTag;
inputInfo.patchedTitle = patchedTitle;
inputInfo.patchedButton = patchedButton;
}
}
}
try {
fixAndroidFileUpload()
} catch(e)
{
alert(e);
}