4

首先,如果有一个问题/答案已经解决了我的问题,那么我真诚地为创建一个新问题而道歉。但是,我已经搜索了3天,还没有找到答案......

我的问题是,我一生都无法弄清楚如何提取文件(任何文件)的内容。通过阅读文档,我发现我返回的文件资源对象应该有一个名为“downloadUrl”的属性,并且我应该能够从中访问文件内容。

返回给我的所有文件资源对象(通过 gapi.client.request)都没有此字段/属性。下面是我用来获取文件的函数。有人可以帮我指出正确的方向吗?我必须在星期一之前完成这个演示,我已经坚持了 2 天......

这是我的 get 函数的代码:

Client.getFileContent = function getFileContent() {
     gapi.client.load('drive', 'v2', function() {
          var request = gapi.client.request({
               path : '/drive/v2/files/1QmaofXyVqnw6ODXHE5KWlUTcWbA9KkLyb-lBdh_FLUs',
               method : 'GET',
               params : {
                    projection: "FULL"
               }
          });
          request.execute(function(response) {
               console.log(response);   
          });
     });
};

返回给我的文件资源对象没有 downloadUrl 属性。

根据要求,这是我为文本文件返回的响应对象。请注意,我用“fileid”替换了一些 id,以便在此处发布。

"kind": "drive#file",
   "id": "fileID",
   "etag": "\"-tJAWr_lbRQU2o8gZ0X7BCBIlVk/MTM0MjYyODQ1MTQ2Nw\"",
   "selfLink": "https://www.googleapis.com/drive/v2/files/fileID",
   "alternateLink": "https://docs.google.com/document/d/fileID/edit",
   "embedLink": "https://docs.google.com/document/d/fileID/preview",
   "thumbnailLink": "https://docs.google.com/feeds/vt?gd=true&id=fileID&v=1&s=AMedNnoAAAAAUAfLhbYIDsNIn40k7DfRYBsrquijmCii&sz=s220",
   "permissionsLink": "https://www.googleapis.com/drive/v2/files/fileID/permissions",
   "title": "Copied filed.txt",
   "mimeType": "application/vnd.google-apps.document",
   "labels": {
    "starred": false,
    "hidden": false,
    "trashed": false,
    "restricted": false,
    "viewed": true
   },
   "createdDate": "2012-07-18T16:20:51.132Z",
   "modifiedDate": "2012-07-18T16:20:51.467Z",
   "modifiedByMeDate": "2012-07-18T16:20:51.467Z",
   "lastViewedByMeDate": "2012-07-18T16:20:51.467Z",
   "parents": [
    {
     "kind": "drive#parentReference",
     "id": "0AAAYYkwdgVqHUk9PVA",
     "selfLink": "https://www.googleapis.com/drive/v2/files/fileID/parents/0AAAYYkwdgVqHUk9PVA",
     "parentLink": "https://www.googleapis.com/drive/v2/files/0AAAYYkwdgVqHUk9PVA",
     "isRoot": true
    }
   ],
   "exportLinks": {
    "application/vnd.oasis.opendocument.text": "https://docs.google.com/feeds/download/documents/export/Export?id=fileID&exportFormat=odt",
    "application/msword": "https://docs.google.com/feeds/download/documents/export/Export?id=fileID&exportFormat=doc",
    "text/html": "https://docs.google.com/feeds/download/documents/export/Export?id=fileID&exportFormat=html",
    "application/rtf": "https://docs.google.com/feeds/download/documents/export/Export?id=fileID&exportFormat=rtf",
    "text/plain": "https://docs.google.com/feeds/download/documents/export/Export?id=fileID&exportFormat=txt",
    "application/pdf": "https://docs.google.com/feeds/download/documents/export/Export?id=fileID&exportFormat=pdf"
   },
   "userPermission": {
    "kind": "drive#permission",
    "etag": "\"-tJAWr_lbRQU2o8gZ0X7BCBIlVk/9STkNeCmz61YXorH3hoJimnEgfM\"",
    "id": "current",
    "role": "owner",
    "type": "user"
   },
   "quotaBytesUsed": "0",
   "ownerNames": [
    "Joshua.morine"
   ],
   "lastModifyingUserName": "Joshua.morine",
   "editable": true,
   "writersCanShare": true
  }
4

2 回答 2

8

对于原生 Google 文档(Google 电子表格、演示文稿等),我们不提供 downloadUrl,因为这些文档无法真正下载为原生格式的文件。相反,您必须使用exportLinks列表中的 URL 之一,它提供了以几种不同的导出格式下载 Google 文档的 URL。

在您的情况下,可以使用以下 Google 文档:

"exportLinks": {
    "application/vnd.oasis.opendocument.text": "https://docs.google.com/feeds/download/documents/export/Export?id=fileID&exportFormat=odt",
    "application/msword": "https://docs.google.com/feeds/download/documents/export/Export?id=fileID&exportFormat=doc",
    "text/html": "https://docs.google.com/feeds/download/documents/export/Export?id=fileID&exportFormat=html",
    "application/rtf": "https://docs.google.com/feeds/download/documents/export/Export?id=fileID&exportFormat=rtf",
    "text/plain": "https://docs.google.com/feeds/download/documents/export/Export?id=fileID&exportFormat=txt",
    "application/pdf": "https://docs.google.com/feeds/download/documents/export/Export?id=fileID&exportFormat=pdf"
   }
于 2012-07-21T13:48:41.247 回答
3

您正在寻找的元数据功能实际上是:

request = gapi.client.drive.files.get({
    'fileId': fileId
});

这会使用您所指的 downloadUrl 产生结果。然后很容易使用任何 HTTP 请求来获取文件。

于 2013-04-26T19:13:40.247 回答