Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set download file name #156

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ Each `object` of `album` array inside your component may contains 3 properties :
| src | Required | The source image to your thumbnail that you want to with use lightbox when user click on `thumbnail` image |
| caption | Optional | Your caption corresponding with your image |
| thumb | Optional | Source of your thumbnail. It is being used inside your component markup so this properties depends on your naming. |
| downloadUrl | Optional | Download url other than `src`. For ex. high resolution image url. |
| downloadName | Optional | Name for downloaded file. If it's not set, it will be taken from `src` url. |

3. Listen to lightbox event

Expand Down
1 change: 1 addition & 0 deletions src/lightbox-event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface IAlbum {
caption?: string;
thumb: string;
downloadUrl?: string;
downloadName?: string;
}

export const LIGHTBOX_EVENT = {
Expand Down
17 changes: 13 additions & 4 deletions src/lightbox.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,19 @@ export class LightboxComponent implements OnInit, AfterViewInit, OnDestroy, OnIn

public download($event: any): void {
$event.stopPropagation();
const url = this.album[this.currentImageIndex].src;
const downloadUrl = this.album[this.currentImageIndex].downloadUrl;
const parts = url.split('/');
const fileName = parts[parts.length - 1];

const file = this.album[this.currentImageIndex];
const url = file.src;
const downloadUrl = file.downloadUrl;

let fileName = '';
if (file.downloadName) {
fileName = file.downloadName;
} else {
const parts = url.split('/');
fileName = parts[parts.length - 1];
}

const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const preloader = new Image();
Expand Down