(Any Website): Download File

(Any Website): Download File

(任意Webサイト): ファイル ダウンロード

This item downloads a file and adds it to File-type data. A GET request is made to the specified URL and the response is saved as a file.

Auto Step icon
Basic Configs
Step Name
Note
Configs for this Auto Step
conf_DataIdA
C1: File URL *#{EL}
conf_OAuth2
C2: Basic Auth Setting / OAuth2 Setting
conf_DataIdB
C3: Data Item to add the file (File) *
conf_SaveAs
C4: Downloaded File Name *#{EL}

Notes

  • (On and after version 14.1) For downloads that require Basic Authentication, 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 auto step

function main() {
  //// == Config Retrieving / 工程コンフィグの参照 ==
  const oauth2 = configs.getObject("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 {AuthSettingWrapper} oauth2  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 );
}

Discover more from Questetra Support

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

Continue reading