Search code examples
javascriptgoogle-apigoogle-picker

JS Google Picker on file upload change file name


I have created a google file picker and the user can either select an existing file or upload a new one.

I have got it working where the user can select or upload a file. However I want to define the filename myself if a user wishes to upload a file.

This is what I currently have:

function createPicker() {
    if (pickerApiLoaded && oauthToken) {
        let picker = new google.picker.PickerBuilder().
            enableFeature(google.picker.Feature.SUPPORT_TEAM_DRIVES).
            addView(new google.picker.DocsView().
                setEnableTeamDrives(true).
                setIncludeFolders(true).
                setParent("FOLDER-ID")
            ).
            addView(new google.picker.DocsUploadView().
                setParent("FOLDER-ID")
            ).
            setOAuthToken(oauthToken).
            setDeveloperKey(developerKey).
            setCallback(pickerCallback).
            hideTitleBar().
            build();
        picker.setVisible(true);
    }
}

// A simple callback implementation.
function pickerCallback(data) {
    let id = 'nothing';
    if (data[google.picker.Response.ACTION] === google.picker.Action.PICKED) {
        let doc = data[google.picker.Response.DOCUMENTS][0];
        id = doc[google.picker.Document.ID];
        console.log(doc);
    }

    let message = 'You picked: ' + id;
    document.getElementById('result').innerHTML = message;
}

I have had a look at the documentation here but I can't see anything there.

I don't know if this is even possible.


Solution

  • Instead I had to update the name manually if the user decided the upload a file.

    // A simple callback implementation.
    function pickerCallback(data) {
        console.log("Data", data);
    
        let doc = 'nothing';
        if (data.action === "picked") {
            doc = data.docs[0];
    
            console.log("Doc", doc);
        }
    
        let message = `You picked: ${doc.id}`;
        document.getElementById('result').innerHTML = message;
    
        if (data.viewToken[0] === "upload") {
            updateName(doc, "888");
        }
    }
    
    // Update the name of the file
    function updateName(doc, newName) {
        newName += "." + doc.name.split(".")[1];
    
        let load = "https://content.googleapis.com/discovery/v1/apis/drive/v3/rest";
        gapi.client.load(load)
            .then(function () {
                return gapi.client.drive.files.update({
                    "fileId": doc.id,
                    "supportsTeamDrives": "true",
                    "resource": {
                        "name": newName
                    }
                })
                    .then(function(response) {
                        // Handle the results here (response.result has the parsed body).
                            console.log("Response", response);
                        },
                        function(err) {
                            console.error("Execute error", err);
                        }
                    );
                });
        }