簡體   English   中英

使用C#從Google驅動器下載文件

[英]Download file from google drive using c#

我正在嘗試通過C#從Google驅動器下載文件。 但是,每次我運行用於下載文件的代碼,但都無法下載時。 這是我的下載代碼:

private static void DownloadFile(Google.Apis.Drive.v3.DriveService driveService)
        {

            var fileId = "//my file id for file on drive";
            var request = driveService.Files.Get(fileId);
            var stream = new System.IO.MemoryStream();
            // Add a handler which will be notified on progress changes.
            // It will notify on each chunk download and when the
            // download is completed or failed.
            request.MediaDownloader.ProgressChanged +=
                (IDownloadProgress progress) =>
                {
                    try
                    {


                        switch (progress.Status)
                        {
                            case DownloadStatus.Downloading:
                                {
                                    Console.WriteLine(progress.BytesDownloaded);
                                    break;
                                }
                            case DownloadStatus.Completed:
                                {
                                    Console.WriteLine("Download complete.");
                                    break;
                                }
                            case DownloadStatus.Failed:
                                {
                                    Console.WriteLine("Download failed.");
                                    break;
                                }
                        }
                    }

                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                };
            request.Download(stream);

        }

代替下載內容,使用webContentLink或在File Get方法中返回Download Url。 您可以僅使用該鏈接來下載文件。

有關更多信息,請檢查文件資源對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM