With standard form submission and when browser goes into the download state, there is no way to programmatically determine the end of form submission.
Because of this, some of the functions tied to this event stop working as intended, for example, submit-lock
feature does not unlock form.
With this library and additional improvements on the backend, you can overcome this.
import 'form-plus/features/catch-download/register-with-shims';
<form action="/download" method="post" data-submit-lock="true" data-catch-download="true">
<select name="file">
<option value="1">Create file 1 (~10s)</option>
<option value="2">Create file 2 (~10s)</option>
</select>
<button>Download</button>
</form>
The script will add a hidden field _requestId
into form.
On backend you must add cookie _requestId{requestId}
with value 1
, where {requestId}
is the passed value in the _requestId
parameter.
submit button -> form -> default settings
For example:
<form action="/download" method="post" data-submit-lock="true" data-catch-download='{"enabled": true, "timeout": 10000}'>
<select name="file">
<option value="1">Create file 1 (~10s)</option>
<option value="2">Create file 2 (~10s)</option>
</select>
<button data-catch-download='{"param": "_req"}'>Download</button>
</form>
You can also change the settings programmatically in submitbefore
event handler. Calculated settings store in e.detail.catchDownload
property.
There are several entry points for importing a library:
import CatchDownload from 'form-plus/features/catch-download'
- easy import without polyfills for ie11, register is required;import CatchDownload from 'form-plus/features/catch-download/with-shims'
- import with shims for ie11, register is required;import CatchDownload from 'form-plus/features/catch-download/with-polyfills'
- import with polyfill for ie11, register is required;import CatchDownload from 'form-plus/features/catch-download/register'
- import without polyfills for ie11, auto-register;import CatchDownload from 'form-plus/features/catch-download/register-with-shims'
- import with shims for ie11, auto-register;import CatchDownload from 'form-plus/features/catch-download/register-with-polifills'
- import with polyfill for ie11, auto-register.
Differences shims from polyfills you can read in polyshim package.
When directly include the script from the dist
folder to the browser, you can get an CatchDownload instance via window.FormPlus.CatchDownload
.
When importing a package without register, you need to register it. When registering, you can set default settings:
import CatchDownload from 'form-plus/features/catch-download/with-shims';
CatchDownload.register({timeout: 15000});
register([settings])
- register librarysettings {Object} [{}]
- settings@return {Object}
- returns the current settings
unregister()
- unregister librarygetRequestId()
- return current request id@return {Number}
setShim([setObjectAssign])
- sets shims for non-cross-browser methodssetObjectAssign {Function|null}
- shim forObject.assign
enabled {boolead} [false]
- enable behaviorparam {String} ['_requestId']
- name of parameter to add form before requestinterval {Number} [100]
- cookie check intervaltimeout {Number} [30000]
- waiting timeout. If a timeout occurs, eventsubmitend
is generated with parametere.datail.timeout = true
;namespace {string} ['catchDownload']
- namespace of settings, see data-settingsshorthands {Object|null} [null]
- list of shorthands for settings, see data-settings