2016-07-07 126 views
0

我需要在谷歌驅動器中創建子文件夾使用谷歌驅動器API添加使用nuget包在控制檯應用程序。谷歌驅動器API創建子文件夾

我可以得到根文件夾的文件夾ID。可以得到rot文件夾的孩子,也可以在根文件夾中上傳文件。唯一的問題是在文件夾中創建子文件夾。

for (int i = 1; i < array.Count(); i++) 
         { 
          var subfoldername = new Google.Apis.Drive.v2.Data.File { Title = array[i], MimeType = "application/vnd.google-apps.folder" }; 
          ChildrenResource.ListRequest request = service.Children.List(rootfolderid); 
          ChildList children = request.Execute(); 
          if (children.Items.Count > 0) 
          { 
           foreach (ChildReference c in children.Items) 
           { 
            Google.Apis.Drive.v2.Data.File file = service.Files.Get(c.Id).Execute(); 
            if (file.MimeType == "application/vnd.google-apps.folder") 
            { 
             List<GoogleDriveFile> googledrive = new List<GoogleDriveFile>(); 
             googledrive.Add(new GoogleDriveFile 
             { 
              OriginalFilename = file.OriginalFilename 
             }); 
            } 
           } 
          } 
          else 
          { 
// here need to add sub folder in folder, but this line adds folder at root 
           var result = service.Files.Insert(foldername).Execute(); 
          } 

回答

0

這裏是我做的,創造了谷歌驅動器中的子文件夾時的方式它必須需要一個父母。因此,在執行字符串q之前,我們需要搜索父根ID

 string findRootId = "mimeType = 'application/vnd.google-apps.folder' and title ='" + RootFolder + "' and trashed = false"; 

      IList<File> _RootId = GoogleDriveHelper.GetFiles(service, findRootId); 

    if (_RootId.Count == 0) 
          { 
           _RootId.Add(GoogleDriveHelper.createDirectory(service, RootFolder, "", "root")); 
           Console.WriteLine("Root folder {0} was created.", RootFolder); 
          } 
          var id = _RootId[0].Id; 

string Q = "mimeType = 'application/vnd.google-apps.folder' and '" + id + "' in parents and title ='" + GoogleDriveFolderName + "' and trashed = false"; 
1

您必須在創建文件夾時添加屬性父項。

parents[]

包含該文件的父文件夾的集合。 設置此字段將把文件放入所有提供的文件夾中。在插入時,如果沒有提供文件夾,文件將被放置在默認的根文件夾中。

示例代碼:

function createSubFolder() { 
var body = new Object(); 
body.title = 'SubFolder'; 
body.parents = [{'id':'0B5xvxYkWPFpCUjJtZVZiMWNBQlE'}]; 
body.mimeType = "application/vnd.google-apps.folder"; 

console.log(body) 
var request = gapi.client.request({ 
'path': '/drive/v2/files', 
'method': 'POST', 
'body': JSON.stringify(body) 
}); 

request.execute(function(resp) { console.log(resp); }); 
} 

我使用的驅動器V2在JavaScript

希望這有助於

相關問題