C# 使用.NET下载Google文档时遇到问题?
我已经为access google drive API创建了示例web应用程序C# 使用.NET下载Google文档时遇到问题?,c#,.net,google-drive-api,google-docs-api,C#,.net,Google Drive Api,Google Docs Api,我已经为access google drive API创建了示例web应用程序 使用我的google帐户启用google drive文档API 已下载credential.json文件并包含在项目中 用下面的代码将一个文档上传到我的谷歌硬盘 下载文件流 但文件流在随机情况下不能正确下载 参考链接: 如何实现这一点?但文件流在随机情况下无法正确下载。这意味着什么?流程完成后,您是否检查了流的内容?是的,我将流保存为文件。该文件没有反映我的更改System.IO.file.writealBytes(
如何实现这一点?
但文件流在随机情况下无法正确下载。
这意味着什么?流程完成后,您是否检查了流的内容?是的,我将流保存为文件。该文件没有反映我的更改System.IO.file.writealBytes(SavedLocation,stream.ToArray())你能给我们举个例子吗?我用一些值编辑了这个文件。自动保存后,我返回了该文件id以获取流。我将下载的流值保存为文件。但是我编辑的更改没有完全反映在该文件中。您确定没有将其保存到其他文件id吗?您正在加载最新版本吗?
UserCredential credential;
string[] scopes = new string[] { DriveService.Scope.Drive,
DriveService.Scope.DriveAppdata,
DriveService.Scope.DriveFile,
DriveService.Scope.DriveMetadataReadonly,
DriveService.Scope.DriveReadonly,
DriveService.Scope.DriveScripts
};
using (var stream = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "credentials.json", FileMode.Open, FileAccess.ReadWrite))
{
string credPath = AppDomain.CurrentDomain.BaseDirectory + "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
DriveService driveService = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
FilesResource.CreateMediaUpload request;
using (var stream = new System.IO.FileStream(fileName, System.IO.FileMode.Open))
{
request = driveService.Files.Create(fileMetadata, stream, mimeType);
request.Fields = "id";
request.Upload();
}
file = request.ResponseBody;
var request = service.Files.Get(fileId);
var stream = new System.IO.MemoryStream();
request.MediaDownloader.ProgressChanged +=
(IDownloadProgress progress) =>
{
switch (progress.Status)
{
case DownloadStatus.Downloading:
{
break;
}
case DownloadStatus.Completed:
{
var bytearray = stream.ToArray();
System.IO.File.WriteAllBytes(SavedLocation, bytearray );
break;
}
case DownloadStatus.Failed:
{
break;
}
}
};
request.Download(stream);