This series of sample macros demonstrate how to use the File Transfer API to retrieve a list of files, download a file, and upload a file to a 3270 host.
NOTE:You must be logged in and at a ready prompt before running these macros.
List files
This macro demonstrates how to use the File Transfer API to retrieve a list of files on a 3270 host using IND$File transfer. The IND$File transfer object is retrieved from the file transfer factory and then used to obtain an array of HostFile objects from either TSO or CMS.
var macro = createMacro(function*() {
'use strict';
try {
var fileTransfer = fileTransferFactory.getInd$File();
var hostFiles = yield fileTransfer.getHostFileListing();
yield ui.message('Found ' + hostFiles.length + ' files');
if (hostFiles.length > 0) {
var firstFile = hostFiles[0];
var msg1 = 'The catalog name is ' + firstFile.getParent() + '. ';
var msg2 = 'The first file is ' + firstFile.getName();
yield ui.message(msg1 + msg2);
}
} catch (error) {
yield ui.message(error);
}
});
// Run the macro
return macro();
Download file
This macro shows how to use the File Transfer API to download a file from a 3270 host using IND$File transfer. The IND$File transfer object is retrieved from the file transfer factory. In this example the transfer method is set to ASCII to demonstrate use of the setTransferOptions function. The sample macro downloads the first file returned from a call to getHostFileListing by creating a download URI with a call to the getDownloadUrl function.The macro can be used in either a CMS or TSO environment but the choice must be specified on the first line or the code modified slightly for the intended system.
var hostEnvironment = 'CMS'; // 'TSO'
// Construct file path, ie catalog/file.name or catalog/partition/file
function getPath (fileNode) {
var prefix = fileNode.getParent() ? fileNode.getParent() + '/' : '';
return prefix + fileNode.getName();
}
var macro = createMacro(function*() {
'use strict';
try {
var fileTransfer = fileTransferFactory.getInd$File();
// The transferMethod options are 'binary' and 'ascii'
fileTransfer.setTransferOptions({transferMethod: 'ascii'});
// This demo retrieves the first file returned in the listing
var hostFiles = yield fileTransfer.getHostFileListing();
var firstHostFile = hostFiles[0];
if (hostEnvironment === 'CMS') {
yield wait.forText('Ready', new Position(1,1), 5000);
}
// Download
// If you already know the path of the file you want, just pass that to getDownloadURL()
var downloadUrl = fileTransfer.getDownloadURL(getPath(firstHostFile));
// This changes the browser location. You may experience different results on different browsers
window.location = downloadUrl;
// If you want to read the file contents into a variable instead of downloading
// it, you can use jQuery
// var fileContents = yield $.get(downloadUrl);
} catch (error) {
yield ui.message(error);
}
});
// Run the macro
return macro();
Upload file
This macro illustrates how to use the File Transfer API to upload a file to a 3270 host using IND$File transfer. The sample macro prompts the user to choose a file from the local file system by triggering the browser’s file selection dialog. It then retrieves the current catalog on TSO or drive identifier on CMS by calling getHostFileListing. Finally, the sendFile function is called to deliver the selected local file to the host.The macro can be used in either a CMS or TSO environment but the choice should be specified on the first line. In this example, the transfer method is set to ascii; you may want to change this to binary.
var hostEnvironment = 'CMS'; // 'TSO'
// Open the browser's file chooser dialog programmatically
function promptForFileToUpload () {
return new Promise(function (resolve, reject) {
// We are not notified if the user cancels the file chooser dialog so reject after 30 seconds
var timerId = setTimeout(reject.bind(null, 'Timed out waiting for file selection'), 30000);
var fileSelector = document.createElement('input');
fileSelector.setAttribute('type', 'file');
fileSelector.onchange = function (evt) {
var file = evt.target.files[0];
clearTimeout(timerId);
resolve(file);
};
fileSelector.click();
});
}
var macro = createMacro(function*() {
'use strict';
try {
var fileTransfer = fileTransferFactory.getInd$File();
// The transferMethod options are 'binary' and 'ascii'
fileTransfer.setTransferOptions({transferMethod: 'ascii'});
var localFile = yield promptForFileToUpload();
// Retrieve the current catalog name and append the selected file name to it
var hostFiles = yield fileTransfer.getHostFileListing();
var destination = hostFiles[0].getParent() + '/' + localFile.name;
if (hostEnvironment === 'CMS') {
yield wait.forText('Ready', new Position(1,1), 5000);
}
var result = yield fileTransfer.sendFile(localFile, destination);
} catch (error) {
yield ui.message(error);
}
});
// Run the macro
return macro();