TarekRaafat / autoComplete.js

Simple autocomplete pure vanilla Javascript library.
https://tarekraafat.github.io/autoComplete.js
Apache License 2.0
3.93k stars 236 forks source link

Force download on click on autocomplete suggestion #401

Open romainpoirier opened 1 year ago

romainpoirier commented 1 year ago

Thoroughly Describe the solution you'd like Download a file on click on suggestion.

Please provide a few use cases for this feature

  1. The autocomplete is a search box for files
  2. The suggestions could be clicked to directly download the files suggested

Please Describe alternatives you've considered My suggestions are download links, and this is my setup:

events: {
  input: {
    selection: (event) => {
      const selection = event.detail.selection.value;
      autoCompleteJS.input.value = selection.value;
      if (typeof selection.url != 'undefined') window.location.href = selection.url;
    }
  }
}

Even if adding this to the htaccess, the files aren't downloaded:

<FilesMatch "\.(?i:pdf)$">
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</FilesMatch>

If looks like I can't force download from window.location.href, so I'm looking a way to act like the download attribute of a <a> link (which is working fine in modern browsers).

trasherdk commented 1 year ago

Open the link in a new window, with response having correct type.

romainpoirier commented 1 year ago

Sorry please can you provide an example? Can't figure out what your're suggesting.

I tried to open in a new window (this is working): window.open(selection.url, '_blank');

But with this in htaccess, the files aren't downloaded (PDF file is still showing instead of downloading):

<FilesMatch "\.(?i:pdf)$">
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</FilesMatch>

With this, I haven't the expected result as I can get using the download attribute on a <a> link.

How to force the response to download it instead of showing it?

trasherdk commented 1 year ago

I did the window.open(selection.url, '_blank') thingy and server-side, in PHP, letting the endpoint set application/octet-stream in the response.