php 在联系表格 7 Wordpress 中提交后下载文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45430803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:07:05  来源:igfitidea点击:

Download File after submission in Contact Form 7 Wordpress

phpwordpresscontact-form-7

提问by Avi

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

我正在尝试制作一种表单,该表单提供诸如用户输入姓名和电子邮件时的功能,然后 PDF 文件开始自动下载。

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?

我目前在本地机器上工作,我知道错误在联系表格 7 邮件选项卡中,但不知道如何解决?

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

“尝试发送您的消息时出错。请稍后再试。”

Here is my contact form 7 code:

这是我的联系表格 7 代码:

<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

这是我在附加设置中编写的代码,用于在提交表单时直接下载 PDF 文件

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

回答by

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

1) 只需将以下代码粘贴到联系表格 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

2) 将下面的代码放在你的 .htacess 文件中,在 [/IfModule] 之后和 #END WordPress 之后

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

回答by Doron Davidowitz

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

如果有人因为in_sent_ok被弃用而正在寻找最新的答案,我们可以使用:

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

回答by Fadi Nouh

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

以下代码对我有用:我用 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');"