Skip to content

Commit

Permalink
Merge pull request #11 from ShiiRochi/direct-url-downloading
Browse files Browse the repository at this point in the history
direct url downloading update
  • Loading branch information
nielsboogaard authored Nov 14, 2023
2 parents b600f97 + 6aec022 commit 94a4d42
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 41 deletions.
34 changes: 22 additions & 12 deletions dist/filepond-plugin-get-file.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,31 @@ const downloadFile = (item, allowDownloadByUrl, downloadFunction) => {
return;
}
// 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();
let isDownloadingDirectly = allowDownloadByUrl && !!item.getMetadata('url');

const a = document.createElement('a');

// item.getMetadate('url') should return full path to remote server is stored in metadata with key 'url'
const url = isDownloadingDirectly
? item.getMetadata('url')
: window.URL.createObjectURL(item.file);

document.body.appendChild(a);
a.style.display = 'none';
a.href = url;

if (isDownloadingDirectly) {
a.target = '_blank';
}

a.download = item.file.name;
a.click();

if (!isDownloadingDirectly) {
window.URL.revokeObjectURL(url);
a.remove();
}

a.remove();
};

/**
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.

33 changes: 18 additions & 15 deletions dist/filepond-plugin-get-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
? (module.exports = factory())
: typeof define === 'function' && define.amd
? define(factory)
: ((global =
typeof globalThis !== 'undefined' ? globalThis : global || self),
(global.FilePondPluginGetFile = factory()));
: ((global = global || self), (global.FilePondPluginGetFile = factory()));
})(this, function () {
'use strict';

Expand Down Expand Up @@ -62,20 +60,25 @@
return;
}
// 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();
let isDownloadingDirectly = allowDownloadByUrl && !!item.getMetadata('url');
const a = document.createElement('a');

// item.getMetadate('url') should return full path to remote server is stored in metadata with key 'url'
const url = isDownloadingDirectly
? item.getMetadata('url')
: window.URL.createObjectURL(item.file);
document.body.appendChild(a);
a.style.display = 'none';
a.href = url;
if (isDownloadingDirectly) {
a.target = '_blank';
}
a.download = item.file.name;
a.click();
if (!isDownloadingDirectly) {
window.URL.revokeObjectURL(url);
a.remove();
}
a.remove();
};

/**
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.

32 changes: 20 additions & 12 deletions src/js/components/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,27 @@ export const downloadFile = (item, allowDownloadByUrl, downloadFunction) => {
return;
}
// 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();
let isDownloadingDirectly = allowDownloadByUrl && !!item.getMetadata('url');

const a = document.createElement("a");

// item.getMetadate('url') should return full path to remote server is stored in metadata with key 'url'
const url = isDownloadingDirectly ? item.getMetadata('url') : window.URL.createObjectURL(item.file);

document.body.appendChild(a);
a.style.display = 'none';
a.href = url;

if (isDownloadingDirectly) {
a.target = '_blank';
}

a.download = item.file.name;
a.click();

if (!isDownloadingDirectly) {
window.URL.revokeObjectURL(url);
a.remove();
}

a.remove();
}

0 comments on commit 94a4d42

Please sign in to comment.