我正在使用Google Drive API从Google Drive下载文件,它可以正常工作,但是我想执行更多暂停和恢复功能。
我阅读了Google的说明页,发现支持部分下载(https://developers.google.com/drive/api/v3/manage-downloads),他们说:“您可以使用带有Range头的字节范围来指定要下载的文件部分”,但我不知道该怎么办,谁可以帮助我?
这是我的代码:
string fileId;
string path;
long bytes;
private void DownloadWorker_DoWork(object sender, DoWorkEventArgs e)
{
DriveService ds = GetService();
var request = ds.Files.Get(fileId);
using (var stream = new MemoryStream())
{
request.MediaDownloader.ProgressChanged += (Google.Apis.Download.IDownloadProgress progress) =>
{
switch (progress.Status)
{
case Google.Apis.Download.DownloadStatus.Downloading:
{
bytes = progress.BytesDownloaded;
GoogleDrive.ReportProgress((int)bytes);
break;
}
case Google.Apis.Download.DownloadStatus.Completed:
{
SaveStream(stream);
break;
}
case Google.Apis.Download.DownloadStatus.Failed:
{
break;
}
}
};
request.Download(stream);
}
}
private void DownloadWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label1.Text = e.ProgressPercentage.ToString();
}
private void DownloadWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
private void SaveStream(MemoryStream stream)
{
long exlength = 0;
if (System.IO.File.Exists(path))
{
var fileInfo = new FileInfo(path);
exlength = fileInfo.Length;
}
var fileMode = exlength > 0 ? FileMode.Append : FileMode.Create;
using (FileStream file = new FileStream(path, fileMode, FileAccess.Write))
{
stream.WriteTo(file);
}
}
private void Button1_Click(object sender, EventArgs e)
{
fileId = "1P2KckTacknNDsONttlzL4GPLSLldzCbA";
path = "D:\\Data.zip";
GoogleDrive.RunWorkerAsync();
}
答案 0 :(得分:0)
已经有一个与您要在此处实现的结果类似的答案[1],但有一个导出请求。基本上,在收到请求之后(发送之前),您需要按照发布的链接中所述设置Range标头。对于您的情况,将是这样的:
DriveService ds = GetService();
var request = ds.Files.Get(fileId);
//Set the range header
request.Headers.Range = new RangeHeaderValue(from, to);
//More code…
//Send the request
request.Download(stream);