Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

183
Views
Download file on button click jquery

Simple question, i think.

I have an html button: <button type="button" class="btn btn-success">Download</button>.

On click i have to start the download of a file placed in the same folder as the project. I've never used anything to download a file so i have no clue how to do it and i'm not understanding from other question either. Could someone tell me how to do it explaining why i have to do it in a certain way? All of this should be preferable in jquery, but even basic js is fine. Thanks.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

using <a> does the job without involving javascript

<a href="./file-path/file-name" class="btn btn-success" download>Download</a>

The download attribute will force download the viewable content. Otherwise, It will be rendered instead of downloading.

about 4 years ago · Juan Pablo Isaza Report

0

Indeed should do the work for you. To better understand the functionality think of the following function

function DownloadUsingAnchorElement(fileUrl, fileName)
{
  const anchor = document.createElement("a");
  anchor.href = fileUrl;
  anchor.download = fileName;
  document.body.appendChild(anchor);
  anchor.click();
  document.body.removeChild(anchor);
}

Check as well : https://www.w3schools.com/tags/att_a_download.asp

about 4 years ago · Juan Pablo Isaza Report

0

document.querySelector('button').addEventListener('click', () => {
  const link = document.createElement('a')
  link.setAttribute('download', 'filename')
  link.setAttribute('href', './path-to-file/filename.ext')
  link.click()
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!