23 lines
768 B
TypeScript
Executable File
23 lines
768 B
TypeScript
Executable File
/// <reference lib="dom" />
|
|
|
|
/**
|
|
* Downloads a file by providing a link.
|
|
* @param href - The URL of the file to download.
|
|
* @param name - The name of the downloaded file. Default is "file".
|
|
* @param target - The target window or frame for the downloaded file. Default is "_blank".
|
|
* @example
|
|
* // Example usage:
|
|
* downloadFileByLink("https://example.com/file.pdf", "myFile", "_self");
|
|
*/
|
|
export const downloadFileByLink = (href: string, name = 'file', target = '_blank'): void => {
|
|
const link = document.createElement('a');
|
|
link.href = href;
|
|
link.download = name;
|
|
link.target = target;
|
|
link.style.visibility = 'hidden';
|
|
link.setAttribute('target', '_blank');
|
|
document.body.appendChild(link);
|
|
link.click();
|
|
document.body.removeChild(link);
|
|
};
|