(Any Website): Download File

(Any Website): Download File
This item 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.
Configs: Common
  • Step Name
  • Note
Configs
  • C1: File URL *#{EL}
  • C2: Basic Auth Setting / OAuth2 Setting
  • C3: Data Item to add the file (File) *
  • C4: Downloaded File Name *#{EL}

Notes

  • (On and after version 14.1) For downloads that Basic Authentication is required, add the setting in HTTP authorization settings

Capture

See also

Script (click to open)
  • An XML file that contains the code below is available to download
    • any-website-file-download.xml (C) Questetra, Inc. (MIT License)
    • If you are using Professional, you can modify the contents of this file and use it as your own add-on

main();
function main() {
  //// == Config Retrieving / 工程コンフィグの参照 ==
  const oauth2 = configs.get("conf_OAuth2");
  
  const fileUrl = configs.get( "conf_DataIdA" );
  const dataIdB = configs.get( "conf_DataIdB" );
  const saveAs  = configs.get( "conf_SaveAs" );

  //// == Data Retrieving / ワークフローデータの参照 == 
  let processFiles = engine.findDataByNumber( dataIdB );
  // java.util.ArrayList <com.questetra.bpms.core.event.scripttask.QfileView>
  if (processFiles === null) {
    processFiles = new java.util.ArrayList();
  }

  //// == Calculating / 演算 ==
  if( saveAs === "" ){
    throw "Downloaded File Name isn't set";
  }
  if ( ! fileUrl.startsWith('http') ) {
    throw "Invalid URL. URL must start with either http or https.";
  }

  const response = accessToTheUrl( oauth2, fileUrl );

  const qfile = saveFile( saveAs, response );

  //// == Data Retrieving / ワークフローデータへの代入 == 
  updateData( processFiles, dataIdB, qfile )
}

/**
  * ダウンロードファイルの URL に GET リクエストを送信し、ファイルを取得する
  * @param {String} oauth2
  * @param {String} fileUrl  ダウンロードファイルの URL
  * @return {HttpResponseWrapper} response  レスポンス
  */
function accessToTheUrl( oauth2, fileUrl ) {
  let response;
  
  try {
    let httpRequest = httpClient.begin()
    if ( oauth2 !== "" && oauth2 !== null ) {
      httpRequest = httpRequest.authSetting(oauth2);
    }
    response = httpRequest.get( fileUrl );
  } catch (e) {
      throw `Unable to access ${fileUrl}.`;
  } 

  const httpStatus  = response.getStatusCode();
  engine.log( `STATUS: ${httpStatus}` );
  if (httpStatus >= 300) {
    engine.log( response.getResponseAsString() );
    throw `Failed to download. STATUS: ${httpStatus}`;
  }

  return response;
}


/**
  * ダウンロードしたファイルを名前を付けて保存する
  * @param {String} saveAs  保存する際のファイル名
  * @param {HttpResponseWrapper} response  レスポンス
  * @return {Qfile} qfile  ファイル
  */
function saveFile( saveAs, response ) {
  const qfile = new com.questetra.bpms.core.event.scripttask.NewQfile(
    saveAs, response.getContentType(), response.getResponse()
  );
  return qfile;
}

/**
  * ダウンロードしたファイルをデータ項目に出力する
  * @param {Array<Qfile>} processFiles  ファイルの配列
  * @param {String} dataIdB  ダウンロードファイルを追加保存するデータ項目のデータ定義番号
  * @param {Qfile} qfile  ファイル
  */
function updateData( processFiles, dataIdB, qfile ) {
  processFiles.add( qfile );
  engine.setDataByNumber( dataIdB, processFiles );
}

2 thoughts on “(Any Website): Download File”

  1. Pingback: HTTP Files Retrieve – Questetra Support

  2. Pingback: File-Type – Questetra Support

Leave a Reply

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

Discover more from Questetra Support

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top