Skip to content

Commit

Permalink
Merge pull request #1 from jevgenijsp/master
Browse files Browse the repository at this point in the history
Added possibility to download file by URL
  • Loading branch information
nielsboogaard authored Jul 24, 2019
2 parents a08450d + b6e226e commit e60f39f
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 95 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ The label of the download icon can be adjusted as follows:
```js
const pond = FilePond.create({
name: 'filepond',
labelButtonDownloadItem: 'custom label' // by default 'Download file'
labelButtonDownloadItem: 'custom label', // by default 'Download file'
allowDownloadByUrl: false, // by default downloading by URL disabled
});
```

Expand Down
53 changes: 37 additions & 16 deletions dist/filepond-plugin-get-file.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@
/**
* Register the download component by inserting the download icon
*/
const registerDownloadComponent = (item, el, labelButtonDownload) => {
const registerDownloadComponent = (
item,
el,
labelButtonDownload,
allowDownloadByUrl
) => {
const info = el.querySelector('.filepond--file-info-main'),
downloadIcon = getDownloadIcon(labelButtonDownload);

info.prepend(downloadIcon);
downloadIcon.addEventListener('click', () => downloadFile(item));
downloadIcon.addEventListener('click', () =>
downloadFile(item, allowDownloadByUrl)
);
};

/**
Expand All @@ -30,18 +37,23 @@ const getDownloadIcon = labelButtonDownload => {
/**
* Triggers the actual download of the uploaded file
*/
const downloadFile = item => {
// create a temporary hyperlink to force the browser to download the file
const a = document.createElement('a');
const url = window.URL.createObjectURL(item.file);
document.body.appendChild(a);
a.style.display = 'none';
a.href = url;
a.download = item.file.name;
a.click();

window.URL.revokeObjectURL(url);
a.remove();
const downloadFile = (item, allowDownloadByUrl) => {
// if client want to download file from remote server
if (allowDownloadByUrl && item.getMetadata('url')) {
location.href = item.getMetadata('url'); // full path to remote server is stored in metadata with key 'url'
} else {
// create a temporary hyperlink to force the browser to download the file
const a = document.createElement('a');
const url = window.URL.createObjectURL(item.file);
document.body.appendChild(a);
a.style.display = 'none';
a.href = url;
a.download = item.file.name;
a.click();

window.URL.revokeObjectURL(url);
a.remove();
}
};

/**
Expand Down Expand Up @@ -71,7 +83,15 @@ const plugin = fpAPI => {
}

const labelButtonDownload = root.query('GET_LABEL_BUTTON_DOWNLOAD_ITEM');
registerDownloadComponent(item, root.element, labelButtonDownload);

const allowDownloadByUrl = root.query('GET_ALLOW_DOWNLOAD_BY_URL');

registerDownloadComponent(
item,
root.element,
labelButtonDownload,
allowDownloadByUrl
);
};

// start writing
Expand All @@ -94,7 +114,8 @@ const plugin = fpAPI => {
// expose plugin
return {
options: {
labelButtonDownloadItem: ['Download file', Type.STRING]
labelButtonDownloadItem: ['Download file', Type.STRING],
allowDownloadByUrl: [false, Type.BOOLEAN]
}
};
};
Expand Down
2 changes: 1 addition & 1 deletion dist/filepond-plugin-get-file.esm.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 34 additions & 15 deletions dist/filepond-plugin-get-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@
/**
* Register the download component by inserting the download icon
*/
const registerDownloadComponent = (item, el, labelButtonDownload) => {
const registerDownloadComponent = (
item,
el,
labelButtonDownload,
allowDownloadByUrl
) => {
const info = el.querySelector('.filepond--file-info-main'),
downloadIcon = getDownloadIcon(labelButtonDownload);
info.prepend(downloadIcon);
downloadIcon.addEventListener('click', () => downloadFile(item));
downloadIcon.addEventListener('click', () =>
downloadFile(item, allowDownloadByUrl)
);
};
/**
* Generates the download icon
Expand All @@ -38,17 +45,22 @@
* Triggers the actual download of the uploaded file
*/

const downloadFile = item => {
// create a temporary hyperlink to force the browser to download the file
const a = document.createElement('a');
const url = window.URL.createObjectURL(item.file);
document.body.appendChild(a);
a.style.display = 'none';
a.href = url;
a.download = item.file.name;
a.click();
window.URL.revokeObjectURL(url);
a.remove();
const downloadFile = (item, allowDownloadByUrl) => {
// if client want to download file from remote server
if (allowDownloadByUrl && item.getMetadata('url')) {
location.href = item.getMetadata('url'); // full path to remote server is stored in metadata with key 'url'
} else {
// create a temporary hyperlink to force the browser to download the file
const a = document.createElement('a');
const url = window.URL.createObjectURL(item.file);
document.body.appendChild(a);
a.style.display = 'none';
a.href = url;
a.download = item.file.name;
a.click();
window.URL.revokeObjectURL(url);
a.remove();
}
};

/**
Expand Down Expand Up @@ -78,7 +90,13 @@
const labelButtonDownload = root.query(
'GET_LABEL_BUTTON_DOWNLOAD_ITEM'
);
registerDownloadComponent(item, root.element, labelButtonDownload);
const allowDownloadByUrl = root.query('GET_ALLOW_DOWNLOAD_BY_URL');
registerDownloadComponent(
item,
root.element,
labelButtonDownload,
allowDownloadByUrl
);
}; // start writing

view.registerWriter(
Expand All @@ -98,7 +116,8 @@

return {
options: {
labelButtonDownloadItem: ['Download file', Type.STRING]
labelButtonDownloadItem: ['Download file', Type.STRING],
allowDownloadByUrl: [false, Type.BOOLEAN]
}
};
}; // fire pluginloaded event if running in browser, this allows registering the plugin when using async script tags
Expand Down
2 changes: 1 addition & 1 deletion dist/filepond-plugin-get-file.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 32 additions & 45 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@babel/preset-env": "^7.4.2",
"autoprefixer": "^9.5.0",
"cssnano": "^4.1.10",
"node-sass": "^4.11.0",
"node-sass": "^4.12.0",
"postcss-cli": "^6.1.2",
"prettier": "^1.16.4",
"rollup": "^1.7.0",
Expand Down
Loading

0 comments on commit e60f39f

Please sign in to comment.