2013-07-12 74 views
0

我想從Google Drive.for下載文檔,以便使用Google Drive API。我對Google Drive API非常陌生,所以任何人都可以告訴我如何從Google Drive下載文檔?從Google Drive api下載文件時出現問題.net

我試着從here這個選項,但是我得到的例外在線

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(file.DownloadUrl.ToString())); 

authenticator.ApplyAuthenticationToRequest(request); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

基礎連接已關閉: 一個發送發生意外的錯誤。

無法從傳輸連接讀取數據:現有的 連接被遠程主機強制關閉。

我遇到這個問題是因爲範圍。請告訴我如何設置範圍在應用

PLS不是我創造的C#.NET

桌面應用程序可以在任何一個可以幫助我?

+0

看起來您的套接字因某些網絡問題而斷開連接。由於範圍錯誤,問題不會發生,我們會以403您的未經授權的人類可讀的錯誤消息作出迴應。可能包含堆棧跟蹤? –

+0

stacktrace like:at System.Net.HttpWebRequest.GetResponse() at GoogleDriveSamples.DriveCommandLineSample.DownloadFile(IAuthenticator authenticator,File file)in D:\ Misc \ RandDwork \ Google Drive \ google-drive-v2-rev82-csharp-1.4 .0-beta \ drive \ GoogleDriveApplication \ GoogleDriveApplication \ Program.cs:第80行和InnerException像:{「無法從傳輸連接讀取數據:現有連接被遠程主機強制關閉。」} – Viral

+0

感謝您的重播但每次都給出相同的例外,所以我認爲網絡問題不存在,請注意,我能夠上傳文件,也能夠獲取元數據,但無法下載文件 – Viral

回答

0

我使用這個代碼從谷歌下載文件的驅動器

public void Download(object sender, DoWorkEventArgs e) 
     { 
      List<File> driveFiles = Google.Apis.Util.Utilities.RetrieveAllFiles(service); 
      int fileCount = driveFiles.Count; 
      int i = 0; 
      IAuthenticator authenticator = new CloudManager().CreateAuthentication(); 
      foreach (var driveFile in driveFiles.Where(driveFile => driveFile.MimeType != "video/mp4")) 
      { 
       LabelFileProcess.Dispatcher.BeginInvoke(DispatcherPriority.Background, 
                 (Action) (() => LabelFileProcess.Content = driveFile.Title)); 
       string title = driveFile.Title; 
       LabelFileProcess.Dispatcher.BeginInvoke(DispatcherPriority.Normal, 
                 (Action) (() => LabelFileProcess.Content = title)); 
       if (string.IsNullOrEmpty(driveFile.Title)) 
       { 
        MessageBox.Show(@"File's title is emplty"); 
        continue; 
       } 

       if (driveFile.MimeType != "application/vnd.google-apps.folder") 
       { 

        Stream stream = Utilities.DownloadFile(authenticator, driveFile); 
        if (stream != null) 
         Utilities.SaveFile(stream, driveFile.Title); 
       } 
       else 
        Directory.CreateDirectory("D:\\GdriveFiles\\" + driveFile.Title); 
      } 

主下載處理方法

public void Download(object sender, DoWorkEventArgs e) 
     { 
      List<File> driveFiles = Google.Apis.Util.Utilities.RetrieveAllFiles(service); 
      int fileCount = driveFiles.Count; 
      int i = 0; 
      IAuthenticator authenticator = new CloudManager().CreateAuthentication(); 
      foreach (var driveFile in driveFiles.Where(driveFile => driveFile.MimeType != "video/mp4")) 
      { 
       LabelFileProcess.Dispatcher.BeginInvoke(DispatcherPriority.Background, 
                 (Action) (() => LabelFileProcess.Content = driveFile.Title)); 
       string title = driveFile.Title; 
       LabelFileProcess.Dispatcher.BeginInvoke(DispatcherPriority.Normal, 
                 (Action) (() => LabelFileProcess.Content = title)); 
       if (string.IsNullOrEmpty(driveFile.Title)) 
       { 
        MessageBox.Show(@"File's title is emplty"); 
        continue; 
       } 

       if (driveFile.MimeType != "application/vnd.google-apps.folder") 
       { 

        Stream stream = DownloadFile(authenticator, driveFile); 
        if (stream != null) 
         SaveFile(stream, driveFile.Title); 
       } 
       else 
        Directory.CreateDirectory("D:\\GdriveFiles\\" + driveFile.Title); 
      } 


public static System.IO.Stream DownloadFile(IAuthenticator authenticator, File file) 
     { 
      if (!string.IsNullOrEmpty(file.DownloadUrl)) 
       try 
       { 
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(file.DownloadUrl)); 
        authenticator.ApplyAuthenticationToRequest(request); 
        HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
        return response.StatusCode == HttpStatusCode.OK ? response.GetResponseStream() : null; 
       } 
       catch (Exception e) 
       { 
        System.Windows.Forms.MessageBox.Show("Exception occures " + e.Message); 
       } 
      else 
       System.Windows.Forms.MessageBox.Show(@"File doesn't have any content on Drive, Title: "+file.Title); 
      return null; 
     } 

     public static void SaveFile(System.IO.Stream stream, String title) 
     { 
      StreamReader streamReader = new StreamReader(stream); 
      //System.Windows.MessageBox.Show(streamReader.ToString()); 
      if (stream == null) 
       System.Windows.MessageBox.Show("Error Occured during download"); 
      else 
      { 


       FileStream fileStream = System.IO.File.Create("D:\\GdriveFiles\\" + title); 
       char[] charArray = new char[100]; 
       int count;// = streamReader.Read(arrayByte, 0, 100); 
       //streamReader.Read(arrayByte, 0, (int)stream.Length); 
       //fileStream.Write(arrayByte,0,arrayByte.Length); 
       string incomingMessage = ""; 
       do 
       { 
        try 
        { 
         count = streamReader.Read(charArray, 0, 100); 
         incomingMessage += new string(charArray, 0, count); 
         byte[] byteArray = new byte[charArray.Length]; 
         //byteArray = System.Text.Encoding.Unicode.GetBytes(charArray); 
         byteArray = System.Text.Encoding.ASCII.GetBytes(charArray); 
         fileStream.Write(byteArray, 0, count); 
        } 
        catch (ArgumentException ex) 
        { 
         MessageBox.Show(@"File ended, Exception:" + ex.Message); 
         break; 
        } 
        catch (Exception e) 
        { 
         MessageBox.Show("Exception occure" + e.Message); 
         break; 
        } 

       } while (count > 0); 
       fileStream.Close(); 
       //MessageBox.Show("File Contains are " + incomingMessage); 
      } 
     } 
4

此代碼工作對我來說:

 var stream = service.HttpClient.GetStreamAsync(file.DownloadUrl); 
     var result = stream.Result; 
     using (var fileStream = System.IO.File.Create(filePathToSaveFileOnDisk)) 
     { 
      result.CopyTo(fileStream); 
     } 
相關問題