简体   繁体   English

是否可以添加一个链接来下载一个只能通过在 Facebook 上共享来下载的文件?

[英]Is it possible to add a link to download a file that can only be downloaded by sharing it on Facebook?

Is this scenario possible?这种情况可能吗?

Customer goes to my website, wants to download a PDF technical document that interests them, they click the Download button and a Facebook share window appears to log them in to share it to Facebook.客户访问我的网站,想要下载他们感兴趣的 PDF 技术文档,他们单击“下载”按钮,然后出现一个 Facebook 共享窗口,让他们登录以将其共享到 Facebook。 Once they click Share and it is posted on their wall then the download begins?一旦他们单击“共享”并将其张贴在他们的墙上,那么下载就开始了吗?

Many thanks.非常感谢。

Ian伊恩

UPDATE更新

According to a Facebook new policy, this act is not allowed.根据 Facebook 的新政策,这种行为是不允许的。 Use it at your own risk.使用它需要您自担风险。 I hold no responsibilities for using this.我对使用它不承担任何责任。

Yes, using the JavaScript SDK , it provides a response (it doesn't anymore) We will create an if statement to see if the response has a post_id if yes show the download link else do something else (alert the user, maybe?)是的,使用JavaScript SDK ,它提供response (不再提供)我们将创建一个if语句来查看响应是否有post_id如果是,则显示下载链接,否则执行其他操作(提醒用户,也许?)

DEMO (API 2.0) (not working; revision required)演示(API 2.0)(不工作;需要修改)

DEMO (API 2.7) working Rev#63演示 (API 2.7) 工作 Rev#63

HTML HTML

<div class="container">
    
    <div>
       <p>This file is locked, to unlock and download it, share it</p>
       <p class="hidden">Thanks for sharing, the file is unlocked and ready for download</p>
       <p class="hidden">In order to download this file, you need to share it</p>
    </div>

    <a class="fsl fsl-facebook" href="#" id="facebook-share">
       <span class="fa-facebook fa-icons fa-lg"></span>
       <span class="sc-label">Share on Facebook</span>
    </a>

    <a class="fsl content-download" href="#" id="download-file">
       <span class="fa-download fa-icons fa-lg"></span>
       <span class="sc-label">Download File</span>
    </a>
    
</div>

JavaScript (jQuery) JavaScript (jQuery)

$('#ShareToDownload').on('click', function(e) {
            e.preventDefault();
            FB.ui({
                  display: 'popup',
                  method:  'share',
                  href:    location.href,
                  },
                  /** our callback **/
                  function(response) {
                          if (response && response.post_id) {
                          /** the user shared the content on their Facebook, go ahead and continue to download **/
                          $('#ShareToDownload').fadeOut(function(){ $('#downloadSection').fadeIn() });    
                          } else {
                          /** the cancelled the share process, do something, for example **/
                          alert('Please share this page to download this file'):
                  }
            });     
}); 

UPDATE更新

With the release of API version 2.0 the Feed dialog was deprecated and replaced with the new modern Share Dialog so the above code uses the new Share Dialog随着API 2.0 版的发布, Feed 对话框已被弃用并替换为新的现代共享对话框,因此上述代码使用新的共享对话框

Thank you, works perfect!谢谢,完美运行! I Didn't know how to get the download link from a JSON file, so I did it slightly different, maybe not that safe.我不知道如何从 JSON 文件中获取下载链接,所以我的做法略有不同,也许不是那么安全。

Add this to the section where the response is checked将此添加到检查响应的部分

$.post('optimus.php', { 'fieldname' : 'download', 'value' : 'yes'});

Made a new page where the session is set (optimus.php)创建了一个设置会话的新页面(optimus.php)

<?php
    session_start();
    $_SESSION[$_POST['fieldname']] = $_POST['value'];
?>

Download.php contains the following code Download.php 包含以下代码

<?php
session_start();
if($_SESSION['download'] ==  "yes"){
session_destroy();
$file = 'file.zip';

    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
    }
} else {
    echo "You didn't share, or you already downloaded the file";
}
?>

So, when the user shared something, the $_SESSION['download'] is set to yes.因此,当用户共享某些内容时,$_SESSION['download'] 设置为 yes。 Download.php checks if it's yes and when it is the download is automatically started. Download.php 检查它是否是,并且当它是时自动开始下载。 Also, the session is destroyed so they can only download once.此外,会话被销毁,因此他们只能下载一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM