Browse Source

FileUtil Methods moved to Abp Window Service

pull/17860/head
Mahmut Gundogdu 3 years ago
parent
commit
6a5f08a97b
  1. 15
      docs/en/UI/Angular/Abp-Window-Service.md
  2. 4
      docs/en/docs-nav.json
  3. 29
      npm/ng-packs/packages/core/src/lib/services/file-utils.service.ts
  4. 3
      npm/ng-packs/packages/core/src/lib/services/index.ts
  5. 20
      npm/ng-packs/packages/core/src/lib/services/window.service.ts

15
docs/en/UI/Angular/File-Utils-Service.md → docs/en/UI/Angular/Abp-Window-Service.md

@ -1,5 +1,8 @@
# File Utils Service
FileUtilsService is an Angular service designed to provide utility methods related to file operations. The service has a `downloadBlob` function, which is used for downloading blobs as files within the context of a web application.
# Abp Window Service
## Download Blob as File
AbpWindowService is an Angular service designed to provide utility methods related to window operations. The service has a `downloadBlob` function, which is used for downloading blobs as files within the context of a web application.
## Usage
@ -9,11 +12,11 @@ Injection:
Firstly, ensure that the service is injected into the component or any other Angular entity where you wish to use it.
```js
import { FileUtilsService } from '@abp/ng.core';
import { AbpWindowService } from '@abp/ng.core';
constructor(private fileUtils: FileUtilsService) { }
constructor(private abpWindowService: AbpWindowService) { }
// or
// private fileUtils = inject(FileUtilsService)
// private abpWindowService = inject(AbpWindowService)
```
Downloading a Blob:
@ -23,7 +26,7 @@ Once you have the service injected, you can use the downloadBlob method to initi
```js
someMethod() {
const myBlob = new Blob(["Hello, World!"], { type: "text/plain" });
this.fileUtils.downloadBlob(myBlob, "hello.txt");
this.abpWindowService.downloadBlob(myBlob, "hello.txt");
}
```

4
docs/en/docs-nav.json

@ -1155,8 +1155,8 @@
"path": "UI/Angular/Content-Security-Strategy.md"
},
{
"text":"File Utils Service",
"path":"UI/Angular/File-Utils-Service.md"
"text":"Abp Window Service",
"path":"UI/Angular/Abp-Window-Service.md"
}
]
},

29
npm/ng-packs/packages/core/src/lib/services/file-utils.service.ts

@ -1,29 +0,0 @@
import { Injectable, inject } from '@angular/core';
import { DOCUMENT } from '@angular/common';
@Injectable({
providedIn: 'root',
})
export class FileUtilsService {
protected readonly document = inject(DOCUMENT);
get window() {
return this.document.defaultView;
}
downloadBlob(blob: Blob, fileName: string) {
const blobUrl = this.window.URL.createObjectURL(blob);
const a = this.document.createElement('a');
a.style.display = 'none';
a.href = blobUrl;
a.download = fileName;
this.document.body.appendChild(a);
a.dispatchEvent(
new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: this.window,
}),
);
this.window.URL.revokeObjectURL(blobUrl);
this.document.body.removeChild(a);
}
}

3
npm/ng-packs/packages/core/src/lib/services/index.ts

@ -20,5 +20,4 @@ export * from './subscription.service';
export * from './track-by.service';
export * from './local-storage.service';
export * from './window.service';
export * from './internet-connection-service'
export * from './file-utils.service'
export * from './internet-connection-service'

20
npm/ng-packs/packages/core/src/lib/services/window.service.ts

@ -3,7 +3,8 @@ import { DOCUMENT } from '@angular/common';
@Injectable({ providedIn: 'root' })
export class AbpWindowService {
protected readonly window = inject(DOCUMENT).defaultView;
protected readonly document = inject(DOCUMENT);
protected readonly window = this.document.defaultView;
protected readonly navigator = this.window.navigator;
copyToClipboard(text: string): Promise<void> {
@ -17,4 +18,21 @@ export class AbpWindowService {
reloadPage(): void {
this.window.location.reload();
}
downloadBlob(blob: Blob, fileName: string) {
const blobUrl = this.window.URL.createObjectURL(blob);
const a = this.document.createElement('a');
a.style.display = 'none';
a.href = blobUrl;
a.download = fileName;
this.document.body.appendChild(a);
a.dispatchEvent(
new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: this.window,
}),
);
this.window.URL.revokeObjectURL(blobUrl);
this.document.body.removeChild(a);
}
}

Loading…
Cancel
Save