C# 如何解决从Google drive下载文件时Google.API.Download.DownloadStatus.Failed的情况
我已经获得了client_secret.json并为C#安装了google API以及OAUTH2库。我能够列出、创建或删除驱动器中的文件和文件夹。但下载失败 我试过几种方法。 1.正在使用文件下载文件。导出 2.使用WebRequest下载文件 在第一种方法中,我得到了Google.api.Download.DownloadStatus.Failed状态,并且文件没有下载 在第二种方法中,所有操作都会通过,并在给定路径上生成一个73KB的无效文件。但我更愿意用另一种方式来讨论这个问题 这总是导致我进入DownloadStatus.Failed,而我希望该方法下载该文件。C# 如何解决从Google drive下载文件时Google.API.Download.DownloadStatus.Failed的情况,c#,google-api,google-drive-api,google-api-dotnet-client,C#,Google Api,Google Drive Api,Google Api Dotnet Client,我已经获得了client_secret.json并为C#安装了google API以及OAUTH2库。我能够列出、创建或删除驱动器中的文件和文件夹。但下载失败 我试过几种方法。 1.正在使用文件下载文件。导出 2.使用WebRequest下载文件 在第一种方法中,我得到了Google.api.Download.DownloadStatus.Failed状态,并且文件没有下载 在第二种方法中,所有操作都会通过,并在给定路径上生成一个73KB的无效文件。但我更愿意用另一种方式来讨论这个问题 这总是导
我在finally块中处理流,以便在操作完成之前不会将其销毁。这是我个人使用的代码,它与您的代码有点不同
private static void DownloadFile(Google.Apis.Drive.v3.DriveService service, Google.Apis.Drive.v3.Data.File file, string saveTo)
{
var request = service.Files.Get(file.Id);
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 += (Google.Apis.Download.IDownloadProgress progress) =>
{
switch (progress.Status)
{
case Google.Apis.Download.DownloadStatus.Downloading:
{
Console.WriteLine(progress.BytesDownloaded);
break;
}
case Google.Apis.Download.DownloadStatus.Completed:
{
Console.WriteLine("Download complete.");
SaveStream(stream, saveTo);
break;
}
case Google.Apis.Download.DownloadStatus.Failed:
{
Console.WriteLine("Download failed.");
break;
}
}
};
request.Download(stream);
}
private static void SaveStream(System.IO.MemoryStream stream, string saveTo)
{
using (System.IO.FileStream file = new System.IO.FileStream(saveTo, System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
stream.WriteTo(file);
}
}
这是我个人使用的代码,与你的有点不同
private static void DownloadFile(Google.Apis.Drive.v3.DriveService service, Google.Apis.Drive.v3.Data.File file, string saveTo)
{
var request = service.Files.Get(file.Id);
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 += (Google.Apis.Download.IDownloadProgress progress) =>
{
switch (progress.Status)
{
case Google.Apis.Download.DownloadStatus.Downloading:
{
Console.WriteLine(progress.BytesDownloaded);
break;
}
case Google.Apis.Download.DownloadStatus.Completed:
{
Console.WriteLine("Download complete.");
SaveStream(stream, saveTo);
break;
}
case Google.Apis.Download.DownloadStatus.Failed:
{
Console.WriteLine("Download failed.");
break;
}
}
};
request.Download(stream);
}
private static void SaveStream(System.IO.MemoryStream stream, string saveTo)
{
using (System.IO.FileStream file = new System.IO.FileStream(saveTo, System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
stream.WriteTo(file);
}
}
我最近遇到了这个错误并找到了解决方案。 我之所以出现此错误,是因为我在尝试从Google drive下载xlsx时使用了
fileResource.GetRequest
而不是fileResource.ExportRequest
对象。这与正在处理的对象有关。我正在下载一个二进制文件(非谷歌对象)。您正在尝试下载Google对象(文件夹),因此请尝试使用GetRequest而不是ExportRequest。例如
希望这有帮助。让我知道它是否有效。我最近遇到了此错误并找到了解决方案。 我之所以出现此错误,是因为我在尝试从Google drive下载xlsx时使用了
fileResource.GetRequest
而不是fileResource.ExportRequest
对象。这与正在处理的对象有关。我正在下载一个二进制文件(非谷歌对象)。您正在尝试下载Google对象(文件夹),因此请尝试使用GetRequest而不是ExportRequest。例如
希望这有帮助。让我知道它是否有效。您好,代码几乎相似。根据您的代码,如果DownloadStatus完成,将调用SaveStream方法。在我的例子中,在调用request.Download(stream)时,**我将下载状态设置为失败。**这是基本问题。嗨,代码几乎相似。根据您的代码,如果DownloadStatus完成,将调用SaveStream方法。在我的例子中,在调用request.Download(stream)时,**我将下载状态设置为失败。**这是基本问题。