【问题标题】:Is it possible to add a link to download a file that can only be downloaded by sharing it on Facebook?是否可以添加链接以下载只能通过在 Facebook 上共享才能下载的文件?
【发布时间】:2013-10-02 05:03:01
【问题描述】:

这种情况可能吗?

客户访问我的网站,想要下载他们感兴趣的 PDF 技术文档,他们单击“下载”按钮,然后会出现一个 Facebook 共享窗口,让他们登录以将其共享到 Facebook。一旦他们点击分享并将其发布在他们的墙上,然后开始下载?

非常感谢。

伊恩

【问题讨论】:

  • 这将违反Platform Policies:“您不得激励用户使用(或阻止使用)Facebook 社交渠道的内容,或暗示激励与使用我们的渠道直接相关。”

标签: javascript facebook facebook-graph-api download sharing


【解决方案1】:

更新

根据 Facebook 的新政策,此行为是不允许的。需要您自担风险使用它。我对使用它不承担任何责任。

是的,使用JavaScript SDK,它提供了一个response(它不再) 我们将创建一个if 语句来查看响应是否有post_id 如果是则显示下载链接否则做其他事情(提醒用户,也许?)

DEMO (API 2.0) (not working; revision required)

DEMO (API 2.7) working Rev#63

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)

$('#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'):
                  }
            });     
}); 

更新

随着 API 版本 2.0 的发布,Feed 对话框已被弃用,取而代之的是新的现代 Share Dialog,因此上述代码使用新的 Share Dialog

【讨论】:

  • 非常感谢亚当!这太棒了!
  • 抱歉打扰。这个脚本可用于 API2.5 吗?如果可以,请在此处提供完整代码。您的演示已被禁止。
  • @Wilf,我相信这些方法在 API2.5 中仍然有效。尽管如此,我会用 jsFiddle 解决问题并检查它是否正常工作。感谢您的提醒。
  • @Wilf,我刚收到 jsFiddle 的回复,他们解锁了小提琴。而且,小提琴得到了更新。请检查是否小提琴,请注意您需要修改 iframe 源以允许弹出窗口工作。这是如何做到这一点stackoverflow.com/a/32721395/2151050
  • @AdamAzad,在我分享完内容之前,您的脚本运行良好。下载按钮不显示。请帮忙!
【解决方案2】:

谢谢,完美!我不知道如何从 JSON 文件中获取下载链接,所以我的做法略有不同,可能不太安全。

将此添加到检查响应的部分

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

创建一个设置会话的新页面(optimus.php)

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

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";
}
?>

因此,当用户分享内容时,$_SESSION['download'] 设置为 yes。 Download.php 检查是否是,何时自动开始下载。此外,会话被销毁,因此它们只能下载一次。

【讨论】:

    猜你喜欢
    • 2017-10-06
    • 2014-02-14
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    相关资源
    最近更新 更多