下載媒體
透過集合功能整理內容 你可以依據偏好儲存及分類內容。
自 1.4.0-beta 版起,Google API .NET 用戶端程式庫一直提供支援續傳的媒體下載功能。 Google API 專屬程式庫包含便利的方法,可與這些 API 互動 這項功能
支援續傳的媒體下載通訊協定與支援續傳的媒體上傳通訊協定類似, 說明 Drive API 的媒體上傳頁面。
主要感興趣的類別是 MediaDownloader
。 在這個可續傳的媒體下載方式中,媒體內容會以分塊形式下載 (區塊大小可自行設定)。
程式碼範例
如果 API 專屬程式庫中的方法包含 「supportsMediaDownload
」參數, 那麼Download
和DownloadAsync
要求類別提供了便利的方法。 這些方法會將媒體資料下載到 Stream
您提供的物件資訊例如:{
// Create the service using the client credentials.
var storageService = new StorageService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "APP_NAME_HERE"
});
// Get the client request object for the bucket and desired object.
var getRequest = storageService.Objects.Get("BUCKET_HERE", "OBJECT_HERE");
using (var fileStream = new System.IO.FileStream(
"FILE_PATH_HERE",
System.IO.FileMode.Create,
System.IO.FileAccess.Write))
{
// 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.
getRequest.MediaDownloader.ProgressChanged += Download_ProgressChanged;
getRequest.Download(fileStream);
}
}
static void Download_ProgressChanged(IDownloadProgress progress)
{
Console.WriteLine(progress.Status + " " + progress.BytesDownloaded);
}
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-03-20 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-03-20 (世界標準時間)。"],[[["The Google API .NET client library supports resumable media download, allowing for the retrieval of large files in manageable chunks."],["Resumable downloads function similarly to resumable uploads, offering a reliable mechanism for handling interruptions during data transfer."],["Developers can utilize the `MediaDownloader` class and convenient `Download`/`DownloadAsync` methods within API-specific libraries to initiate and manage these downloads, providing a stream for the received data."],["The download process is configurable, enabling customization of chunk size and progress tracking through events like `ProgressChanged` for monitoring the transfer status."]]],[]]