2016-05-19 4 views
4

Как получить одно имя файла из запроса на получение с помощью диска api?Google Drive V3 Api получить имя файла, C#

Я сделал запрос, но там нет метаданных о файле, я могу его загрузить.

var fileId = "0BwwA4oUTeiV1UVNwOHItT0xfa2M"; 
var request = driveService.Files.Get(fileId); 

Видимо, это возвращает files.get в ответ в соответствии с этим doc

Я просто хочу, чтобы загрузить файл и отображается его имя, а не только его идентификатор

+0

Посмотрите здесь: http://stackoverflow.com/questions/23063691/how-to-get-file-name-and-real-path-of-google-drive-document –

ответ

0

Попробуйте

/// 
    /// Download a file 
    /// Documentation: https://developers.google.com/drive/v2/reference/files/get 
    /// 
    /// a Valid authenticated DriveService 
    /// File resource of the file to download 
    /// location of where to save the file including the file name to save it as. 
    /// 
    public static Boolean downloadFile(DriveService _service, File _fileResource, string _saveTo) 
    { 

     if (!String.IsNullOrEmpty(_fileResource.DownloadUrl)) 
     { 
      try 
      { 
       var x = _service.HttpClient.GetByteArrayAsync(_fileResource.DownloadUrl); 
       byte[] arrBytes = x.Result; 
       System.IO.File.WriteAllBytes(_saveTo, arrBytes); 
       return true;     
      } 
      catch (Exception e) 
      { 
       Console.WriteLine("An error occurred: " + e.Message); 
       return false; 
      } 
     } 
     else 
     { 
      // The file doesn't have any content stored on Drive. 
      return false; 
     } 
    } 

Код, снятый с моего Google drive api C# download учебник. Который я не обновлял в возрасте, поэтому, если есть какие-либо вопросы, дайте мне знать, и я их исправлю.

+0

Можно ли смешивать вызовы API V3 с вызовами V2 api? – FillyPajo

+0

Не совсем идеальный Я постараюсь найти код v3, он должен быть очень похож на v2. Извините, что я не читаю прочтение. Я все время забываю о новом api – DaImTo

1

Вы можете получить имя файла из Title собственности в File класса:

string FileName = service.Files.Get(FileId).Execute().Title; 

и для загрузки,

// DriveService _service: a valid Authendicated DriveService 
// Google.Apis.Drive.v2.Data.File _fileResource: Resource of the file to download. (from service.Files.Get(FileId).Execute();) 
// string _saveTo: Full file path to save the file 

public static void downloadFile(DriveService _service, File _fileResource, string _saveTo) 
{ 
    if (!String.IsNullOrEmpty(_fileResource.DownloadUrl)) 
    { 
     try 
     { 
      var x = _service.HttpClient.GetByteArrayAsync(_fileResource.DownloadUrl); 
      byte[] arrBytes = x.Result; 
      System.IO.File.WriteAllBytes(_saveTo, arrBytes); 
     } 
     catch(Exception e) 
     { 
      MessageBox.Show(e.Message, "Error Occured", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      Environment.Exit(0); 
     } 
    } 
} 
1

для Google Drive V3:

C#:
string f = driveService.Files.Get(fileId).Execute().Name;

VB:
Dim f As String = driveService.Files.Get(fileId).Execute().Name