最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it possible to add a link to download a file that can only be downloaded by sharing it on Facebook? - Stack Over

programmeradmin3浏览0评论

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. Once they click Share and it is posted on their wall then the download begins?

Many thanks.

Ian

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. Once they click Share and it is posted on their wall then the download begins?

Many thanks.

Ian

Share Improve this question edited Jan 4, 2017 at 11:48 Adam Azad 11.3k5 gold badges30 silver badges72 bronze badges asked Oct 2, 2013 at 5:03 Ian WalkerIan Walker 1032 silver badges7 bronze badges 1
  • That would violate Platform Policies: “You must not incentivize users to use (or gate content behind the use of) Facebook social channels, or imply that an incentive is directly tied to the use of our channels.” – C3roe Commented Oct 2, 2013 at 7:07
Add a comment  | 

2 Answers 2

Reset to default 19

UPDATE

According to a Facebook new policy, this act is not allowed. 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?)

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

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

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.

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)

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

Download.php contains the following code

<?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. Download.php checks if it's yes and when it is the download is automatically started. Also, the session is destroyed so they can only download once.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论