简体   繁体   中英

create public folder and allow user to create files into the particular folder

I am creating an application using asp.net mvc and javascript in which I am creating folders and creating document inside those folders,

below is my code to create folders and docs

function createFolder() {
    loadClient('https://www.googleapis.com/discovery/v1/apis/drive/v3/rest', function () {
        var body = {
            'name': document.getElementById('txtFolderName').value,
            'mimeType': "application/vnd.google-apps.folder",
            'parents': ['1R7c6lCV_qRxSTVHnbefLN4vJY0ZTMFUW']
        };

        return gapi.client.drive.files.create({
            'resource': body
        }).then(function (response) {
            console.log("Response", response);
        }, function (err) { console.error("Execute error", err); });
    })
}

function execute() {
    loadClient('https://docs.googleapis.com/$discovery/rest?version=v1', function () {
        return gapi.client.docs.documents.create({
            "resource": {
                "title": document.getElementById('txtTitle').value
            }
        })
            .then(function (response) {
                // Handle the results here (response.result has the parsed body).
                console.log("Response", response);
            },
                function (err) { console.error("Execute error", err); });
    })
}

now I want to create folders which will be accessible to everyone of my organization and insert the file in those folder

for example I have folder Named as "My company" and when I create a file, the file should be created inside "My company" folder

thanks for your time

I believe your goal is as follows.

  • You want to create a new folder and want to publicly share the created folder as the writer.
  • You want to achieve this using googleapis for Javascript.

In this case, how about the following modification?

Modified script:

From:

return gapi.client.drive.files.create({
    'resource': body
}).then(function (response) {
    console.log("Response", response);
}, function (err) { console.error("Execute error", err); });

To:

gapi.client.drive.files.create({ 'resource': body })
.then(function (response) {
  console.log("Response", response);

  // Created folder is publicly shared.
  var folderId = response.result.id;
  gapi.client.drive.permissions.create({
    "fileId": folderId,
    "resource": {"role": "writer", "type": "anyone" }
  })
  .then(function (res) {
    console.log(res);
  }, function (err) {
    console.log(err.result.error.message);
  });

  // Create new Google Document to the created folder.
  var body = {
    'name': "sample filename",
    'mimeType': "application/vnd.google-apps.document",
    'parents': [folderId]
  };
  gapi.client.drive.files.create({ 'resource': body })
  .then(function (res) {
    console.log(res);
  }, function (err) {
    console.log(err.result.error.message);
  });

}, function (err) {
  console.error("Execute error", err);
});
  • I added the script for one more thing I want to achieve is that I want to create document in those a particular folder of my google drive .

Reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM