How to download file instead of opening in new tab?

Asked

Viewed 138 times

0

I have a page with several PDF files and when I click on some it opens a modal with a form to fill, after the confirmation of sending the file opens twice in a new tab, I would need that it does not open 2 times, but yes that it downloads automatically.

var fileLink, modalForm;
var showModal = true;

jQuery(function() {

    // Modal
    modalForm = jQuery('#modal');

    jQuery('.post-attachments')
        .find('a')
        .click(function() {

            fileLink = jQuery(this);

                  modalForm.modal(); 
                
        

            return false;


        })



});

document.addEventListener( 'wpcf7mailsent', function( event ) {
    
    downloadFile()
    
    }, false );

function downloadFile() {
    

    showModal = false;
    
    
        window.open(fileLink.attr('href'));
        modalForm.modal('hide');


}

1 answer

0


Add the download attribute to your tag

<a href="exemplo.png" download>

if you want to put a suggestion name in the image

<a href="/path/to/image.png" download="exemplo.png">
  • Is there any way to do it directly in js? Without having to add the download attribute to the tag.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.