Hi Don,
Johan’s code has some errors in them. Too many arguments and the last 2 arguments are switched.
The syntax is:
showFileOpenDialog(selectionMode, startDirectory, multiselect, filter, callbackfunction, title)
So Johan’s code:
plugins.file.showFileOpenDialog(1, null, false, null, null, 'Select the import file', mycallbackfunction);
Should be:
plugins.file.showFileOpenDialog(1, null, false, null, mycallbackfunction, 'Select the import file');
Anyway, here is some (tested) code that does what you are looking for I think.
The first method ‘btnUpload’ shows the upload dialog that calls ‘uploadCallbackFunction’ when the user triggers the upload.
This method will stream the file to the filesystem of the server from where it gets imported line by line by the callback function ‘doImportFile’.
/**
* @param {JSEvent} event the event that triggered the action
*/
function btnUpload(event) {
// show the upload dialog and call the callback function when the user his Upload
plugins.file.showFileOpenDialog(1, null, false, null, uploadCallbackFunction, 'Select the import file');
}
/**
* @param {plugins.file.JSFile} _oFile
*/
function uploadCallbackFunction(_oFile) {
// Streaming the file to the server and call the callback method when this is done
plugins.file.streamFilesToServer(_oFile, doImportFile);
}
/**
* @param {plugins.file.JSFile} _oFile
*/
function doImportFile(_oFile) {
// We need to add the upload path defined in the Servoy-Admin pages to the filename
_oFile = plugins.file.getDefaultUploadLocation() + "/" + _oFile.getName();
//
// Use BufferedReader so we don't have to read the whole file into memory
//
var _oFR = new Packages.java.io.FileInputStream(_oFile),
_oIR = new Packages.java.io.InputStreamReader(_oFR, "UTF8"),
_oBR = new Packages.java.io.BufferedReader(_oIR),
_sLine = "dummy",
_nReadLine = 0;
// using a database transaction (might/will) speed things up
databaseManager.startTransaction();
try {
while (_sLine) {
_sLine = _oBR.readLine();
_nReadLine++;
if (_sLine) {
// Put your processing code here
}
}
// Save any unsaved data
databaseManager.saveData();
//
//do NOT forget this close! to prevent memory leaks
//
_oBR.close();
// Close the database transaction
databaseManager.commitTransaction();
} catch (_oErr) {
_oBR.close();
application.output("ERROR: " + _oFile.getName() + " at row " + _nReadLine, LOGGINGLEVEL.ERROR);
application.output("ERROR: " + _oErr, LOGGINGLEVEL.ERROR);
databaseManager.rollbackTransaction();
} finally {
//
// garbage collection
//
_oFR = null;
_oIR = null;
_oBR = null;
}
}
Hope this helps.