/******************************************************************* Module : /assets/js/download.js Desc. : Fungsi-fungsi download data & laporan Created By : Siau Pheng (siaupheng@3fonia.com). Created Date : April 1st, 2011. Last Modified : October 2nd, 2026. (c) 3FONIA Software; WWW.3FONIA.COM. *******************************************************************/ var Download = function(options) { var _options = { 'url': '', 'filename': 'file.bin', 'title': '', 'text': 'share dari aplikasi' }; var _params = []; this.construct = function(options){ if (typeof options == 'object') { $.extend(_options, options); } else _options.url = options; }; this.set_url = function (url){ _options.url = url; }; this.add_param = function(par_id, par_val) { var _getvalue = (par_val) ? par_id + '=' + encodeURIComponent(par_val) : par_id; _params.push(_getvalue); }; this._exec = async function(is_share) { if (!_options.url) { console.warn('cek url !'); return false; } try { const blob = await $.ajax({ type: 'POST', url: _options.url, data: FoniaJS.RequestParam(_params), xhrFields: { responseType: 'blob' }, success: async function (data, status, xhr) { const disposition = xhr.getResponseHeader('Content-Disposition'); if (disposition && disposition.indexOf('attachment') !== -1) { const filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/; const matches = filenameRegex.exec(disposition); if (matches != null && matches[1]) { _options.filename = matches[1].replace(/['"]/g, ''); } } } }); const file = new File([blob], _options.filename, { type: blob.type }); if (is_share === true && navigator.canShare && navigator.canShare({ files: [file] })) { await navigator.share({ files: [file], title: _options.title ?? _options.filename, text: _options.text }); } else { const link = document.createElement('a'); link.href = URL.createObjectURL(blob); link.download = _options.filename; link.click(); } } catch (error) { console.error('Download / sharing gagal :', error); }; return this; }; this.share = async function() { await this._exec(true); }; this.download = async function() { await this._exec(false); }; this.construct(options); };