-2

What I want achieve

All I would like to achieve is that implement a file download functionality such as image files, text files etc... using javascript or Jquery.

estimated procedure is like..

① select a file ② click download icon ③ start download file ← here is my problem.

below codes are that I tried.

<a target="_blank"  href="{{ f.file }}" download="{{ f.title }}" download><i class="fa fa-download text-secondary border p-2" aria-hidden="true"></i></a>

estimated browser crome, firefox.

Any of ideas would be great. ※ so far, below code works fine except for image files.

<a href="<your_file_url>" title="{{ f.title }}" download></a>

Thanks.

Ryoma Kishimoto
  • 425
  • 1
  • 6
  • 16
  • You're trying to use two `download` attributes. There should only be one. Aside from that, what isn't working? What does this have to do with JavaScript or jQuery? – David Dec 07 '19 at 12:18
  • Possible duplicate: [Download image with JavaScript](https://stackoverflow.com/questions/17311645/download-image-with-javascript) – Rob Dec 07 '19 at 12:27
  • 1
    Is there any reason to use javascript at all? File download is a basic browser functionality that should be possible to achieve with a simple link, probably adding target=_blank. Also be sure that your web server sends the correct file type and, if necessary, set Content-Disposition: attachment, see https://stackoverflow.com/questions/9195304/how-to-use-content-disposition-for-force-a-file-to-download-to-the-hard-drive – Ingo Steinke Dec 07 '19 at 12:27

1 Answers1

0

Try:

<a href="<your_file_url>" title="{{ f.title }}" download></a>

Just be sure you give your file URL in href. You are using ‘"{{f.file}}"’. First check if {{f.file}} redirects to the right file URL.

Sandeep Kumar
  • 2,397
  • 5
  • 30
  • 37
Joker09
  • 93
  • 7
  • Thank you for your reply and sample code. code you gave worked for download files but image files. Did you know any reasone with this.???? – Ryoma Kishimoto Dec 07 '19 at 12:53
  • your URL should hit your database to access the file you want to download. So, href should have the right URL. And it works for every file(txt, excel, image files), just be sure about the path URL. – Joker09 Dec 07 '19 at 13:01
  • Actually those files stored in google cloud storage, I think that`s why download does not work as expected. – Ryoma Kishimoto Dec 07 '19 at 13:33
  • I don't think this can be a cause. If URL is correct then it should download the requested file. – Joker09 Dec 09 '19 at 04:48