(Any Website): File Download
Downloads a file and adds to file-type data. The GET request to the URL is thrown and the response is saved as a file. It is also possible to rename the file name.
https://support.questetra.com/addons/any-website-file-download/
2019-01-07 (C) Questetra, Inc. (MIT License)
Configs
  • A: Select STRING/SELECT DATA for File URL *
  • B: Select FILE DATA for Downloaded File (update) *
  • Bx: Set the file name if you want to save as a different name #{EL}
  • C: Select STRING DATA for Content Type (update)
  • D: Select NUMERIC DATA for Byte Size (update)
Script
//// == Config Retrieving / 工程コンフィグの参照 ==
const dataIdA = configs.get( "conf_DataIdA" ) + "";
const dataIdB = configs.get( "conf_DataIdB" ) + "";
const saveAs  = configs.get( "conf_SaveAs" ) + "";
const dataIdC = configs.get( "conf_DataIdC" ) + "";
const dataIdD = configs.get( "conf_DataIdD" ) + "";
// convert 'java.lang.String' to 'javascript string'


//// == Data Retrieving / ワークフローデータの参照 ==
let fileUrl = "";
if( engine.findDataDefinitionByNumber( dataIdA ).matchDataType( "SELECT_SINGLE" ) ){
    fileUrl = engine.findDataByNumber( dataIdA ).get(0).getValue() + "";
}else{
    fileUrl = engine.findDataByNumber( dataIdA ) + "";
}
if ( ! fileUrl.startsWith('http') ) {
  throw new Error( '\n Invalid URL. URL must start with either http or https. \n' );
}


let processFiles = engine.findDataByNumber( dataIdB );
// java.util.ArrayList <com.questetra.bpms.core.event.scripttask.QfileView>
if (processFiles === null) {
  processFiles = new java.util.ArrayList();
}


//// == Calculating / 演算 ==
/// File Name (from URI Path with Regular Expression)
let fileName = "";
if( saveAs === "" ){
  fileName = fileUrl.match(".+/(.+?)([\?#;].*)?$")[1];
  if( fileName.endsWith('/') ){ fileName = "NoName"; }
}else{
  fileName = saveAs;
}

/// Access to the URL
let httpRequest = httpClient.begin(); // HttpRequestWrapper
// com.questetra.bpms.core.event.scripttask.HttpClientWrapper
const response = httpRequest.get( fileUrl ); // HttpResponseWrapper
const httpStatus  = response.getStatusCode() + "";
engine.log( "STATUS: " + httpStatus );
const qfile = new com.questetra.bpms.core.event.scripttask.NewQfile(
    fileName, response.getContentType(), response.getResponse()
  );

/// Error Handling
// (no action)


//// == Data Updating / ワークフローデータへの代入 ==
processFiles.add( qfile );
engine.setDataByNumber( dataIdB, processFiles );

if( dataIdC !== "" ){
  engine.setDataByNumber( dataIdC, response.getContentType() );
}
if( dataIdD !== "" ){
  engine.setDataByNumber( dataIdD, new java.math.BigDecimal(qfile.getLength()) );
} // java.lang.Long -> java.math.BigDecimal

Download

any-website-file-download.xml

Capture

See also

1 thought on “(Any Website): File Download”

  1. Pingback: HTTP Files Retrieve – Questetra Support

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.