简体   繁体   中英

Google Drive API v3 .NET: How to let users download files directly from the google drive and not from the server?

I'm working on an asp.net website with google drive api v3 and still a newbie. Everything is working well with the google drive api. But I'm not really happy with my current way of downloading files.

Here is the sample code:

    public static string DownloadGoogleFile(string fileId)
    {
            DriveService service = GetService(); //Get google drive service
            FilesResource.GetRequest request = service.Files.Get(fileId);

            string FileName = request.Execute().Name;                
            string FilePath = Path.Combine(HttpContext.Current.Server.MapPath("~/Downloads"),FileName);                

            MemoryStream stream = new MemoryStream();

            request.MediaDownloader.ProgressChanged += (Google.Apis.Download.IDownloadProgress progress) =>
            {
                switch (progress.Status)
                {
                    case DownloadStatus.Downloading:
                        {
                            Console.WriteLine(progress.BytesDownloaded);                                
                            break;
                        }
                    case DownloadStatus.Completed:
                        {
                            Console.WriteLine("Download complete.");
                            SaveStream(stream, FilePath); //Save the file 
                            break;
                        }
                    case DownloadStatus.Failed:
                        {
                            Console.WriteLine("Download failed.");
                            break;
                        }
                }
            };
            request.Download(stream);                

            return FilePath;
    }

This is event handler in code behind:

protected void btnDownload_Click(object sender, EventArgs e)
{

    try
    {
        string id = TextBox3.Text.Trim();
        string FilePath = google_drive.DownloadGoogleFile(id);

        HttpContext.Current.Response.ContentType = MimeMapping.GetMimeMapping(FilePath);
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + System.IO.Path.GetFileName(FilePath));
        //HttpContext.Current.Response.WriteFile(FilePath);          
        HttpContext.Current.Response.TransmitFile(FilePath);
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.End();
    }        
    catch (Exception ex)
    {
        //Handle Exception
    }
}

So when a user request to download a file from the google drive, server will download the file first and then transfer it back to the user. I'm wondering if there is a way we can let the users download files through a browser directly from the google drive and not from the server. And if there's not, I think I should delete downloaded files on the server after users downloaded. Please enlighten me. Thanks a lot in advance.

Downloading the file directly from browser would be to use the webContentLink .

A link for downloading the content of the file in a browser. This is only available for files with binary content in Drive."

Apparently, you're using ASP but I'll just share a snippet on how I do it with JS when I use the Drive Picker:

 function downloadImage(data) {
      if (data.action == google.picker.Action.PICKED) {
        var fileId = data.docs[0].id;
        //for images
        // var webcontentlink = 'https://docs.google.com/a/google.com/uc?id='+fileId+'&export=download'

        var webcontentlink = ' https://docs.google.com/uc?id='+fileId+'&export=download'
       window.open( webcontentlink,'image/png');
      }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM