Is there a link to GitHub for downloading a file in the latest release of a repository?

前端 未结 18 2329
情深已故
情深已故 2020-12-02 04:26

Using GitHub\'s Release feature, it is possible to provide a link to download a specific version of the published software. However, every time a release is made, the gh-pag

18条回答
  •  一个人的身影
    2020-12-02 05:15

    You can do an ajax request to get latest release download URL using the GitHub Releases API. It also shows when it was released and the download count:

    function GetLatestReleaseInfo() {
      $.getJSON("https://api.github.com/repos/ShareX/ShareX/releases/latest").done(function(release) {
        var asset = release.assets[0];
        var downloadCount = 0;
        for (var i = 0; i < release.assets.length; i++) {
          downloadCount += release.assets[i].download_count;
        }
        var oneHour = 60 * 60 * 1000;
        var oneDay = 24 * oneHour;
        var dateDiff = new Date() - new Date(asset.updated_at);
        var timeAgo;
        if (dateDiff < oneDay) {
          timeAgo = (dateDiff / oneHour).toFixed(1) + " hours ago";
        } else {
          timeAgo = (dateDiff / oneDay).toFixed(1) + " days ago";
        }
        var releaseInfo = release.name + " was updated " + timeAgo + " and downloaded " + downloadCount.toLocaleString() + " times.";
        $(".download").attr("href", asset.browser_download_url);
        $(".release-info").text(releaseInfo);
        $(".release-info").fadeIn("slow");
      });
    }
    
    GetLatestReleaseInfo();
    
    Download
    

    It is important for you to set the default button URL to the releases page (like https://github.com/ShareX/ShareX/releases/latest) so if the browser does not support ajax (or javascript) or is too slow to get the URL, the download button will still work.

    When the Ajax request completes, the URL of this button will change automatically to a direct download URL.

    Edit:

    I also made a downloads page that shows multiple releases which you can find here: https://getsharex.com/downloads/

    Source code of it: https://github.com/ShareX/sharex.github.io/blob/master/js/downloads.js

提交回复
热议问题