C#-以字节为单位从Google云端硬盘下载 [英] C# - Downloading from Google Drive in byte chunks
问题描述
我目前正在为网络连接较差的环境进行开发.我的应用程序可帮助自动为用户下载所需的Google云端硬盘文件.它适用于较小的文件(从40KB到2MB),但对于较大的文件(9MB)却经常失败.我知道这些文件的大小似乎很小,但是就我的客户端的网络环境而言,Google Drive API始终无法使用9MB的文件.
I'm currently developing for an environment that has poor network connectivity. My application helps to automatically download required Google Drive files for users. It works reasonably well for small files (ranging from 40KB to 2MB), but fails far too often for larger files (9MB). I know these file sizes might seem small, but in terms of my client's network environment, Google Drive API constantly fails with the 9MB file.
我得出的结论是,我需要以较小的字节块下载文件,但是我看不到如何使用Google Drive API来完成.我一遍又一遍地阅读了此,并且我尝试了以下代码:
I've concluded that I need to download files in smaller byte chunks, but I don't see how I can do that with Google Drive API. I've read this over and over again, and I've tried the following code:
// with the Drive File ID, and the appropriate export MIME type, I create the export request
var request = DriveService.Files.Export(fileId, exportMimeType);
// take the message so I can modify it by hand
var message = request.CreateRequest();
var client = request.Service.HttpClient;
// I change the Range headers of both the client, and message
client.DefaultRequestHeaders.Range =
message.Headers.Range =
new System.Net.Http.Headers.RangeHeaderValue(100, 200);
var response = await request.Service.HttpClient.SendAsync(message);
// if status code = 200, copy to local file
if (response.IsSuccessStatusCode)
{
using (var fileStream = new FileStream(downloadFileName, FileMode.CreateNew, FileAccess.ReadWrite))
{
await response.Content.CopyToAsync(fileStream);
}
}
但是,生成的本地文件(来自fileStream
)仍然是完整长度的(即40KB驱动器文件为40KB文件,而9MB文件为500内部服务器错误).在旁注中,我也尝试了ExportRequest.MediaDownloader.ChunkSize
,但是从我观察到的结果来看,它仅更改了ExportRequest.MediaDownloader.ProgressChanged
回调的调用频率(即,如果ChunkSize
设置为256 * 1024,则回调将触发每256KB ).
The resultant local file (from fileStream
) however, is still full-length (i.e. 40KB file for the 40KB Drive file, and a 500 Internal Server Error for the 9MB file). On a sidenote, I've also experimented with ExportRequest.MediaDownloader.ChunkSize
, but from what I observe it only changes the frequency at which the ExportRequest.MediaDownloader.ProgressChanged
callback is called (i.e. callback will trigger every 256KB if ChunkSize
is set to 256 * 1024).
我该如何进行?
推荐答案
您似乎正在朝正确的方向前进.根据您的最后一条评论,该请求将根据块大小更新进度,因此您的观察是准确的.
You seemed to be heading in the right direction. From your last comment, the request will update progress based on the chunk size, so your observation was accurate.