0

My problem is having the File Download with Visual Basic .NET using the Google Drive API. When I use FileStream, it creates the file but does not download it fully. It looks 0MB. I want to make download using FileID. Not by URL.

Where is my code error?

Me.Cursor = Cursors.WaitCursor
If Service.ApplicationName <> "vbNETDriveTools" Then CreateService()
Dim Downloader = New MediaDownloader(Service)
Downloader.ChunkSize = 256 * 1024
Dim fileid1 = "DriveFileID"
Dim Request1 = Service.Files.Get(fileid1)
Dim Results = Request1.Execute()
Dim filename = "C:\Users\XXX\Desktop\" & Results.OriginalFilename
Using Stream = New FileStream(filename, FileMode.Create, FileAccess.Write)
    Dim Progress = Downloader.DownloadAsync("which url should I write", Stream)
    Threading.Thread.Sleep(1000)
    Do While Progress.Status = TaskStatus.Running
    Loop
    If Progress.Status = TaskStatus.RanToCompletion Then
        MsgBox("Download Complete!")
    Else
        MsgBox("Download Failed :(")
    End If
End Using
Me.Cursor = Cursors.Default

Are there sample codes that you can help as a Windows Form application?

ugurcan
  • 1
  • 2

2 Answers2

0

The method Downloader.DownloadAsync() requires the usage of an URL instead of the file ID. Check the following information about how to obtain an URL from the fileID for a file on Google Drive.

  1. An answer to a similar question on stackoverflow recommends to create the export link as: https://www.googleapis.com/drive/v3/files/fileIdxxxxx/export&mimeType=xxxxx/xxxxx. Make sure you URL encode the mime type.
  2. The Google Drive Api documentation describes the properties webContentLink and exportLinks, which you can obtain by listing the files and specifying those properties in the parameter fields.
  3. Apps Script features the method getDownloadUrl().

You have to test which of those Links will work for your application.

ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • Thank you very much for your answer, but I don't want to download it by URL – ugurcan Jul 10 '19 at 07:04
  • Then unfortunately you cannot use the function DownloadAsync(), since it requires an URL as parameter: https://developers.google.com/api-client-library/dotnet/reference/1.9.1/classGoogle_1_1Apis_1_1Download_1_1MediaDownloader – ziganotschka Jul 10 '19 at 07:11
  • DownloadAsync () is downloading the first 10MB of my file. What can I use instead? – ugurcan Jul 10 '19 at 08:02
  • Problem with chunksize is the default value of 10MB. What do I need to download this value GB file? – ugurcan Jul 10 '19 at 08:59
  • Have a look if you find useful information in the references: https://developers.google.com/api-client-library/dotnet/reference/1.9.1/classGoogle_1_1Apis_1_1Download_1_1MediaDownloader#a641b76c0708d3b27794dc859bc14c3f1 – ziganotschka Jul 11 '19 at 21:59
  • 1
    Thank you. I solved the problem using Google Drive API v3. – ugurcan Jul 13 '19 at 07:24
0

I don't want to download it by URL. I already find the file with FileID and make the download process. The following code allows me to download only 10MB of files to my desktop(http://prntscr.com/ocpd7x). The same file Google Drive is 350MB. How do I download the entire file? Where is the error in the code?

If Service.ApplicationName <> "vbNETDriveTools" Then CreateService()
Dim fileid As String = "1PVsSbS5qEhuMEtDPeuPsvA_eBDy_XX_C"
Dim Request = Service.Files.Get(fileid)
Dim Results = Request.Execute()
Dim Downloader = New MediaDownloader(Service)
Dim filename = "C:\Users\XXX\Desktop\" & Results.OriginalFilename
Dim Stream As System.IO.FileStream = New FileStream(filename, FileMode.Create, FileAccess.Write)
AddHandler Request.MediaDownloader.ProgressChanged, AddressOf ProgChanged
Request.DownloadAsync(Stream)
ugurcan
  • 1
  • 2