Download File after submission in Contact Form 7 W

2020-08-01 02:18发布

问题:

I am trying to make one form which give functionality like when user enter name and email,then PDF file starts download automatically.

And while i am applying this code in additional setting tab on submit button,it replay the error message like this.

I am currently working in local machine, i know error is in contact form 7 mail tab,but don't know how to fix it?

"There was an error trying to send your message. Please try again later."

Here is my contact form 7 code:

<label> Name
    [text* your-name] </label>

<label> Email 
    [email* your-email] </label>

[submit "Download Now"]

Here is code that i write in Additional Setting for download PDF file directly when form is submitted

on_sent_ok: "location = 'http://localhost/wordpress/wp-content/uploads/2017/08/pdf-sample.pdf';"

回答1:

I Found solution for your need,just follow below stops,it can not send mail,but works fine in local machine as per your requirement.

1) Just paste below code in your Additional Setting tab in contact form 7

demo_mode : on

on_sent_ok: "location = 'http://localhost/wordpress/wp-content/uploads/2017/08/pdf-sample.pdf';"

2) Put below code in your .htacess file, after [/IfModule] and below # END WordPress

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


回答2:

the following code is working for me: I made it with javascript

function force_download( file ) {
    pdf = window.open(file, '', 'left=100,screenX=100');
    pdf.document.execCommand('SaveAs', 'null', 'myfile.pdf');
    pdf.close();
 }
 on_sent_ok: "force_download('pdf_url_here');"


回答3:

If someone is looking for an up-to-date answer since in_sent_ok is being deprecated, instead we can use:

   <script>
    document.addEventListener( 'wpcf7mailsent', function( event ) {
    location = 'http://example.com/';
    }, false );
    </script>