Browse Source

Merge pull request #17860 from abpframework/issue/17854

Add `FileUtilsService` to provide utility methods for file operations
pull/17905/head
Masum ULU 2 years ago
committed by GitHub
parent
commit
b10a4a0d5d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 41
      docs/en/UI/Angular/Abp-Window-Service.md
  2. 4
      docs/en/docs-nav.json
  3. 2
      npm/ng-packs/packages/core/src/lib/services/index.ts
  4. 20
      npm/ng-packs/packages/core/src/lib/services/window.service.ts

41
docs/en/UI/Angular/Abp-Window-Service.md

@ -0,0 +1,41 @@
# 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
To make use of the `AbpWindowService` in your Angular application, follow the steps below:
### Injection
Firstly, ensure that the service is injected into the component or any other Angular entity where you wish to use it.
```js
import { AbpWindowService } from '@abp/ng.core';
constructor(private abpWindowService: AbpWindowService) { }
// or
// private abpWindowService = inject(AbpWindowService)
```
### Downloading a Blob
Once you have the service injected, you can use the downloadBlob method to initiate the download of blob data as a file. For instance:
```js
someMethod() {
const myBlob = new Blob(["Hello, World!"], { type: "text/plain" });
this.abpWindowService.downloadBlob(myBlob, "hello.txt");
}
```
### Permissions & Considerations
Ensure that you have appropriate permissions and user interactions before triggering a download. Since downloadBlob initiates a download programmatically, it's best to tie this action to direct user interactions, such as button clicks, to prevent unexpected behaviors or browser restrictions.
### DOCUMENT Token in Service
Angular, being a platform-agnostic framework, is designed to support not only browser-based applications but also other environments like server-side rendering (SSR) through Angular Universal. This design philosophy introduces challenges when accessing global browser-specific objects like window or document directly. To address this, Angular provides a DOCUMENT token that can be used to inject the document object into Angular entities like components and services.

4
docs/en/docs-nav.json

@ -1153,6 +1153,10 @@
{
"text": "Content Security Strategy",
"path": "UI/Angular/Content-Security-Strategy.md"
},
{
"text":"Abp Window Service",
"path":"UI/Angular/Abp-Window-Service.md"
}
]
},

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

@ -20,4 +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 './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