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

前端 未结 2 619
野的像风
野的像风 2020-11-27 20:39

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

相关标签:
2条回答
  • 2020-11-27 21:03

    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

    0 讨论(0)
  • 2020-11-27 21:07

    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 讨论(0)
提交回复
热议问题