3

I have a radio button that lists file names from my google drive. I want to be able to select a file and download it to my local device using the download url. I am getting the download url using HTTP GET request using the ajax /jquery from the Drive API. Here is the success function which is inside my get request.

    success: function (data) {
                   console.log(data);
                   console.log(data.items[0].alternateLink);

                   $.each(data.items, function(key, value) {
                    
                    const uri = value.alternateLink;
                    var uri_dec = decodeURIComponent(uri);
                    
                    
                    $('ul').append('<li class="list-group-item"> <input type="radio" name="recording" id="recording" value="' +uri_dec+ '">  '+value.title+' </li>');
                    //$("ul").append("<li class=\"list-group-item\">"+value.title+"</li>");
                    console.log(uri_dec);
                    
                    document.getElementById("Decoded").innerHTML = uri_dec;

}}
Maxime Helen
  • 1,382
  • 7
  • 16
Aanya
  • 31
  • 1
  • Does this answer your question? [Download File Using Javascript/jQuery](https://stackoverflow.com/questions/3749231/download-file-using-javascript-jquery) –  Aug 04 '20 at 00:19

2 Answers2

0

do you need to render radio buttons? If not, this snippet will create a list of download links:

const uri = value.downloadUrl;  // try this or value.webContentLink

$('ul').append('<li class="list-group-item"><a href="' + uri + '">'+value.title+' </a></li>');

You should be able to click any of the links to download the file.

Reading the Google Drive API documentation, I think downloadUrl or webContentLink might be the properties you're looking for:

https://developers.google.com/drive/api/v2/reference/files

If you still need help, check out Google's guide:

https://codelabs.developers.google.com/codelabs/gsuite-apis-intro/#0

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ghan
  • 797
  • 8
  • 28
0

you need to add download attribute

const uri = value.downloadUrl;

$('ul').append('<li class="list-group-item"><a href="' + uri + ' download ">'+value.title+' </a></li>');

if you want to give name to your file you can also give value to download attribute like this :-

$('ul').append('<li class="list-group-item"><a href="' + uri + ' download ='theFileName' ">'+value.title+' </a></li>');
Parth B Thakkar
  • 115
  • 2
  • 10