Hi,
I need to download files from google drive and save to a folder on local using C#.
I have done as described in Google Developer API documentation (https://developers.google.com/drive/v3/web/manage-downloads) but i'm getting the file with invalid format. Please suggest how to download it.
I done:
downloadUrl = url of the file (eg: https://drive.google.com/uc?id=1ULNeKdDoCRmWgPPBW8-d1EGUZgqvA1Ul&export=download)
filename = some name with extension
- private bool SaveToDir(string downloadUrl,string filename) {
- string filePath = Server.MapPath("imports/");
- bool resp = false;
- DriveService ds = new DriveService();
- Uri temp = new Uri(downloadUrl);
- string fileId = HttpUtility.ParseQueryString(temp.Query).Get("id");
- var req = ds.Files.Get(fileId.Trim());
- var stream = new MemoryStream();
- req.MediaDownloader.ProgressChanged += (Google.Apis.Download.IDownloadProgress dp)=> {
- switch (dp.Status)
- {
- case Google.Apis.Download.DownloadStatus.Downloading:
- Message("downloading, please wait....");
- break;
- case Google.Apis.Download.DownloadStatus.Completed:
- using (FileStream file = new FileStream(filePath, FileMode.Create, FileAccess.Write))
- {
- stream.WriteTo(file);
- Message("File Downloaded successfully.");
- }
- resp = true;
- break;
- case Google.Apis.Download.DownloadStatus.Failed:
- Message("Failed to Download.");
- resp = false;
- break;
- }
- };
- req.Download(stream);