Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

google drive api create sub folders

I need to create sub folders in google drive using google drive api added using nuget package in console application.

I can get the folder id of root folder. Can get children of rot folder, can also upload file in root folder. Only problem is creation of sub folders in folders.

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();
                            }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You must add the property parents while creating a Folder.

parents[]

Collection of parent folders which contain this file. Setting this field will put the file in all of the provided folders. On insert, if no folders are provided, the file will be placed in the default root folder.

Sample Code:

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); });
}

I'm using Drive v2 in JavaScript

Hope this helps


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...