我正在编写一个程序来下载Google云端硬盘中的所有数据并保存。问题是,当文件被保存时,其原始大小会增加(例如,一个.xls文件的大小为287KB,另存为87411KB的大小),我无法打开它,因为它说它已损坏。大多数文件是.xls和.xlsx文件。
我遵循了本教程以及Google API文档。
现在,我正在使用以下代码:
public static void GetFilesFromDrive()
{
UserCredential credential;
using (var stream =
new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Drive API service.
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define parameters of request.
FilesResource.ListRequest listRequest = service.Files.List();
listRequest.PageSize = 400;
listRequest.Fields = "nextPageToken, files(id, name)";
// List files.
IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute()
.Files;
Console.WriteLine("Files:");
String path = copypath;
var jetStream = new System.IO.MemoryStream();
if (files != null && files.Count > 0)
{
foreach (var file in files)
{
Console.WriteLine("{0} ({1})", file.Name, file.Id);
FilesResource.GetRequest request = new FilesResource.GetRequest(service, file.Id);
//ExportRequest(service, file.Id, "application/vnd.google-apps.file");
//(service, file.Id);
string pathforfiles = path + file.Name.ToString();
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.");
using (System.IO.FileStream file = new System.IO.FileStream(pathforfiles, System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
jetStream.WriteTo(file);
}
break;
}
case Google.Apis.Download.DownloadStatus.Failed:
{
Console.WriteLine("Download failed.");
break;
}
}
};
request.DownloadWithStatus(jetStream);
}
}
else
{
Console.WriteLine("No files found.");
}
//Console.Read();
}
看来您没有搁置内存流。尝试var jetStream = new System.IO.MemoryStream();
进入foreach。
if (files != null && files.Count > 0)
{
foreach (var file in files)
{
var jetStream = new System.IO.MemoryStream();
Console.WriteLine("{0} ({1})", file.Name, file.Id);
FilesResource.GetRequest request = new FilesResource.GetRequest(service, file.Id);
//ExportRequest(service, file.Id, "application/vnd.google-apps.file");
//(service, file.Id);
string pathforfiles = path + file.Name.ToString();
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.");
using (System.IO.FileStream file = new System.IO.FileStream(pathforfiles, System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
jetStream.WriteTo(file);
}
break;
}
case Google.Apis.Download.DownloadStatus.Failed:
{
Console.WriteLine("Download failed.");
break;
}
}
};
request.DownloadWithStatus(jetStream);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句