我在下载过程中遇到问题,它会下载,但下载的文件大小相同:108102字节;无论实际文件是小于还是大于此。
我能够成功上传PDF文件并设置其权限,以便链接https://drive.google.com/open?id=UNIQUE_ID_HERE
的任何人都可以查看该文件。下面是我用来异步下载文件的函数:
/// <summary>Downloads the media from the given URL.</summary>
private async Task DownloadFile(DriveService service, string url)
{
var downloader = new MediaDownloader(service);
downloader.ChunkSize = DownloadChunkSize;
// add a delegate for the progress changed event for writing to console on changes
downloader.ProgressChanged += Download_ProgressChanged;
var fileName = DownloadDirectoryName + @"\cover_new.pdf";
Console.WriteLine("Downloading file from link: {0}", url);
using (var fileStream = new System.IO.FileStream(fileName, System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
var progress = await downloader.DownloadAsync(url, fileStream);
if (progress.Status == DownloadStatus.Completed)
{
Console.WriteLine(fileName + " was downloaded successfully: " + progress.BytesDownloaded);
}
else
{
Console.WriteLine("Download {0} was interrupted in the middle. Only {1} were downloaded. ", fileName, progress.BytesDownloaded);
}
}
}
此外,我可以使用链接从其他浏览器成功打开此文件,而无需输入任何凭据。
答案 0 :(得分:0)
我错误地尝试使用以下网址下载文件:https://drive.google.com/open?id=UNIQUE_ID_HERE
。
我对方法所做的更改:
url
现在是file
个对象。我正在传递网址,但我应该使用service.Files.Get()
并使用其ID来获取文件。downloadfile
用于我想要下载的文件,我可以调用它的DownloadAsync
方法,向它发送我已经拥有的文件流。以下新代码:
private async Task DownloadFile(DriveService service, Google.Apis.Drive.v3.Data.File fileToDownload)
{
var downloader = new MediaDownloader(service);
downloader.ChunkSize = DownloadChunkSize;
// add a delegate for the progress changed event for writing to console on changes
downloader.ProgressChanged += Download_ProgressChanged;
var fileName = DownloadDirectoryName + @"\cover_new.pdf";
var downloadfile = service.Files.Get(fileToDownload.Id);
Console.WriteLine("Downloading file with id: {0}", fileToDownload);
using (var fileStream = new System.IO.FileStream(fileName, System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
var progress = await downloadfile.DownloadAsync(fileStream);
if (progress.Status == DownloadStatus.Completed)
{
Console.WriteLine(fileName + " was downloaded successfully: " + progress.BytesDownloaded);
}
else
{
Console.WriteLine("Download {0} was interrupted in the middle. Only {1} were downloaded. ", fileName, progress.BytesDownloaded);
}
}
}
我希望这可以帮助那些人。