2017-08-10 6 views
1

vb.netでgoogleドライブからファイルをダウンロードしたいと思います。 私はグリッドビューを持っています。すべてのファイルがドライブに存在し、グリッド内のファイルを選択するとダウンロードします。メッセージボックスがsuccefulダウンロードをaffichとき 異なるウェブサイトで検索した後、私はそれをしようが、私はここで問題ドライブからファイルをダウンロードする

を持っているが、私のコード

Public Sub DownloadFile(index As String) 
    CreateService() 
    Dim url As String = "" 
    Dim list = Service.Files.List() 
    ' list.Fields = "nextPageToken, items(id, title)" 
    Dim count = list.Execute() 
    For Each fich In count.Items 
     If (fich.Title = index) Then 
      url = fich.WebContentLink 
      Exit For 
     End If 
    Next 
    Dim Downloader = New MediaDownloader(Service) 
    ' figure out the right file type base on UploadFileName extension 
    Dim Filename = index 
    Dim FileStream = New System.IO.FileStream(Filename, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite) 
    Dim progress = Downloader.DownloadAsync(url, FileStream) 
    If DownloadStatus.Downloading.Completed Then 
     MessageBox.Show("succeful download") 
    Else 
     MessageBox.Show("erreur") 
    End If 

ですが、私は、私のアプリケーションディレクトリ内のファイルを見つけましたしかし、私はそれを開くことができない、彼のサイズが0キロバイト

指数は(グリッドの)値rowselectedです

答えて

1

あなたが最初の非同期ダウンロード方法を使用しているので、あなたが再確認するために、フロントでAwaitを使用する必要がありますメソッドの主体はダウンロード後に実行されます。 しかし、あなたの方法のプロセスとそれ以外のことが間違っている可能性を十分に理解するのに十分なコードを提供していません。

あなたは、ファイルをダウンロードするには、このパターンに従ってください:https://developers.google.com/api-client-library/dotnet/guide/media_download

は、これは一例であり、文字通りに解釈すべきではありません。メソッドにパラメータを追加して、APP_NAME_HERE、BUCKET_HERE、OBJECT_HERE、FILE_PATH_HEREを記入します。

Public Shared Sub DownloadFile() 
    ' Create the service using the client credentials. 
    Dim storageService = New StorageService(New 
    BaseClientService.Initializer() With 
     { 
      .HttpClientInitializer = credential, 
      .ApplicationName = "APP_NAME_HERE" 
     }) 
    ' Get the client request object for the bucket and desired object. 
    Dim getRequest = storageService.Objects.Get("BUCKET_HERE", "OBJECT_HERE") 
    Using fileStream As New System.IO.FileStream(
     "FILE_PATH_HERE", 
     System.IO.FileMode.Create, 
     System.IO.FileAccess.Write) 
     ' Add a handler which will be notified on progress changes. 
     ' It will notify on each chunk download and when the 
     ' download is completed or failed. 
     AddHandler getRequest.MediaDownloader.ProgressChanged, AddressOf Download_ProgressChanged 
     getRequest.Download(fileStream) 
    End Using 
End Sub 

Private Shared Sub Download_ProgressChanged(IDownloadProgress progress) 
    Console.WriteLine(progress.Status & " " & progress.BytesDownloaded); 
End Sub 

に使用していたように、これは、GoogleクラウドAPIを使用するための方法であるが、可能性の高いGoogleドライブのAPIを使用するよりも、あなたがより:

Imports System.Net 
Imports Google.Apis.Authentication 
Imports Google.Apis.Drive.v2 
Imports Google.Apis.Drive.v2.Data 

Public Class MyClass 

    ''' <summary> 
    ''' Download a file and return a stream with its content. 
    ''' </summary> 
    ''' <param name="authenticator"> 
    ''' Authenticator responsible for creating authorized web requests. 
    ''' </param> 
    ''' <param name="file">Drive File instance.</param> 
    ''' <returns>File's content if successful, null otherwise.</returns> 
    Public Shared Function System.IO.Stream DownloadFile(authenticator As IAuthenticator, webFile As File) 

     If Not String.IsNullOrEmpty(webFile.DownloadUrl) Then 
      Try 
       Dim request = WebRequest.Create(New Uri(webFile.DownloadUrl)) 
       authenticator.ApplyAuthenticationToRequest(request) 
       Dim response = request.GetResponse() 
       If response.StatusCode = HttpStatusCode.OK Then 
        Return response.GetResponseStream() 
       End If 

       Console.WriteLine("An error occurred: " & response.StatusDescription) 
       Return null 

      Catch (e as Exception) 
       Console.WriteLine("An error occurred: " & e.Message) 
       Return null 
      End Try 
     End If 

     ' The file doesn't have any content stored on Drive. 
     Return null; 
    End Function 
End Class 

何を持っていることは非常に近いです、これらの変更が有効になっているはずです。

Public Sub DownloadFile(service As DriveService, fileName As String, outputStream As Stream) 
    Dim list = service.Files.List().Execute() 
    Dim file = list.FirstOrDefault(Function(x) x.Title.Equals(fileName)) 
    If file Is Nothing Then 
     Console.WriteLine("File not found.") 
     Return 
    End If 
    Dim request = service.Files.Get(file.Id) 

    ' Add a handler which will be notified on progress changes. 
    ' It will notify on each chunk download and when the 
    ' download is completed or failed. 
    AddHandler request.MediaDownloader.ProgressChanged, 
      Sub(IDownloadProgress progress) 
       Select Case progress.Status 
        Case DownloadStatus.Downloading 
         Console.WriteLine(progress.BytesDownloaded) 

        case DownloadStatus.Completed 
         Console.WriteLine("Download complete.") 

        Case DownloadStatus.Failed: 
         Console.WriteLine("Download failed.") 

       End Select 
      End Sub 
     request.Download(outputStream); 
End Sub 
+0

BUCKET_HERE、OBJECT_HEREの意味は? –

+0

@ Zied.Mこれは、使用しているGoogle Cloud APIへの参照です。 Googleドライブのデータが必要な場合は、他のAPIを使用する必要があります。 – Jeremy

+0

私はGoogle Drive API V2を使用していますが、私の方法は正しいと思いますが結果は偽で、私はあなたの回答を理解していません。ありがとうございました –

関連する問題