64 lines
2.1 KiB
JavaScript
64 lines
2.1 KiB
JavaScript
/*******************************************************************
|
|
|
|
Module : /assets/js/export.js
|
|
Desc. : Fungsi-fungsi export data & laporan
|
|
Created By : Siau Pheng ([email protected]).
|
|
Created Date : April 1st, 2011.
|
|
Last Modified : January 19th, 2026.
|
|
|
|
(c) 3FONIA Software; WWW.3FONIA.COM.
|
|
|
|
*******************************************************************/
|
|
|
|
var cEXPORT = function(url, options){
|
|
var _options = {
|
|
'url': '',
|
|
'filename': ''
|
|
};
|
|
var _params = [];
|
|
|
|
this.construct = function(url, options){
|
|
if (typeof url == 'object') {
|
|
$.extend(_options, url);
|
|
} else {
|
|
$.extend(_options, options);
|
|
_options.url = url;
|
|
}
|
|
};
|
|
this.set_url = function (url){
|
|
_options.url = url;
|
|
};
|
|
this.add_param = function (par_id, par_val){
|
|
var _getvalue = (par_val) ? par_id + "=" + escape(par_val) : par_id;
|
|
_params.push(_getvalue);
|
|
},
|
|
this.download = async function(){
|
|
if (!_options.url) {
|
|
console.warn('cek url !');
|
|
return false;
|
|
}
|
|
var _pro = (window.location.protocol=='https:') ? 'https://' : 'http://';
|
|
var _src = _pro+FoniaJS.WEB_HOST + _options.url + '?' + $.param(FoniaJS.RequestParam(_params));
|
|
|
|
try {
|
|
const response = await fetch(_src)
|
|
.then(response => {
|
|
const cDispo = response.headers.get('Content-Disposition');
|
|
if (cDispo && cDispo.indexOf('filename=') !== -1) {
|
|
_options.filename = cDispo.split('filename=')[1].split(';')[0].replace(/"/g, '');
|
|
}
|
|
return response;
|
|
})
|
|
const blob = await response.blob();
|
|
const link = document.createElement('a');
|
|
link.href = URL.createObjectURL(blob);
|
|
link.download = _options.filename;
|
|
link.click();
|
|
} catch (error) {
|
|
console.error('Download gagal:', error);
|
|
}
|
|
return this;
|
|
};
|
|
this.construct(url, options);
|
|
};
|