Guruprakash C

Guruprakash C

  • NA
  • 142
  • 27.7k

Download files from google drive and save to a folder

Feb 16 2018 1:05 AM
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
  1. private bool SaveToDir(string downloadUrl,string filename) {  
  2. string filePath = Server.MapPath("imports/");  
  3. bool resp = false;  
  4. DriveService ds = new DriveService();  
  5. Uri temp = new Uri(downloadUrl);  
  6. string fileId = HttpUtility.ParseQueryString(temp.Query).Get("id");  
  7. var req = ds.Files.Get(fileId.Trim());  
  8. var stream = new MemoryStream();  
  9. req.MediaDownloader.ProgressChanged += (Google.Apis.Download.IDownloadProgress dp)=> {  
  10. switch (dp.Status)  
  11. {  
  12. case Google.Apis.Download.DownloadStatus.Downloading:  
  13. Message("downloading, please wait....");  
  14. break;  
  15. case Google.Apis.Download.DownloadStatus.Completed:  
  16. using (FileStream file = new FileStream(filePath, FileMode.Create, FileAccess.Write))  
  17. {  
  18. stream.WriteTo(file);  
  19. Message("File Downloaded successfully.");  
  20. }  
  21. resp = true;  
  22. break;  
  23. case Google.Apis.Download.DownloadStatus.Failed:  
  24. Message("Failed to Download.");  
  25. resp = false;  
  26. break;  
  27. }  
  28. };  
  29. req.Download(stream); 
 
 

Attachment: Default-aspx-cs.zip

Answers (1)