forked from bioversity/Crop-Ontology
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fileupload.js
40 lines (36 loc) · 1.26 KB
/
fileupload.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
importPackage(org.apache.commons.fileupload);
importPackage(org.apache.commons.fileupload.servlet);
importPackage(org.apache.commons.io);
importPackage(com.google.appengine.api.datastore);
importPackage(java.lang);
var fileupload = {
getData: function(request) {
var data = [];
try {
var upload = new ServletFileUpload();
var iter = upload.getItemIterator(request);
while(iter.hasNext()) {
var item = iter.next();
var stream = item.openStream();
if(item.isFormField()) {
data.push({
file: false,
fieldName: item.getFieldName(),
// added the "" at the end to convert it to JS string
fieldValue: new java.lang.String(IOUtils.toByteArray(stream), "utf-8")
});
} else {
data.push({
file: true,
fieldName: item.getName(),
fieldValue: new Blob(IOUtils.toByteArray(stream))
});
}
}
} catch (e){
throw new Exception(e);
}
return data;
}
};
exports = fileupload;