0
votes

We are developing a linking mechanism between our web application and Google Drive. We show Google Picker to our users and the user selects a file and we link selected file to our data in the Picker callback via $.post. Everything works well and we are able to get id,name,mimeType,url and sizeBytes of selected file in the Picker callback.

We also want our users to upload files from Picker and it works as well except one thing. User uploads the file, we get data.docs object successfully in Picker callback but this time "sizeBytes" field is undefined.

Do we miss something or do something wrong? Why is "sizeBytes" field undefined after uploaded in Picker api?

This is how we create Google Picker,

var view = new google.picker.View(google.picker.ViewId.DOCS);
var picker = new google.picker.PickerBuilder()
.enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
.setAppId(appId)
.setOAuthToken(oauthToken)            
.addView(view)
.addView(new google.picker.DocsUploadView())
.setLocale(pickerApiLocale)
.setMaxItems(5)
.setCallback(pickerCallback)
.build();
picker.setVisible(true);

Edit:

This is how we fetch callback data;

function pickerCallback(data) {
    if (data.action == google.picker.Action.PICKED) {
        for (var i = 0; i < data.docs.length; i++) {
            var selectedDoc = data.docs[i];
            var params = {
                fileId: selectedDoc.id,
                fileName: selectedDoc.name,
                mimeType: selectedDoc.mimeType,
                webViewLink: selectedDoc.url,
                size: selectedDoc.sizeBytes,
            };
            $.post('myurl',params,function(){
              some code here....
            });
    }
  }
}

If the user uploads a file "sizeBytes" is undefined.

2
How are you fetching the "sizeByte" metadata? Was there a response in the first place to be fetched?noogui
@noogui I edited my questioncck

2 Answers

1
votes

I suspect that Picker is not specifying fields=* on the upload. This means the upload response will only be the minimal file properties. You will need do a files.get(id)&fields=*.

Also note that any Google documents (doc, spreadsheet, etc) will not have a size.

1
votes

This is what's happening. You are accessing "sizeBytes" (which is really fileSize) property from data.doc[0] object but there is no such property there. If you console.log() that Object, here are the properties you are able to access:

{id: "dfdsafdsafdsafdsafM", serviceId: "docs", mimeType: "image/png", name: "IMAGE.png", description: "", …}
description
downloadUrl
driveError
driveSuccess
iconUrl
id
isNew
lastEditedUtc
mimeType
name
serviceId
type
uploadId
uploadState
url

There is no sizeBytes (fileSize) in the list, hence you are getting undefined. If you want to access the fileSize property, you need to use files.get as mentioned in Handling Google Drive Items. And that job my friend, I will leave to you. I've already done the debugging part.