Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google api 作为字符串返回的文件_Google Api_Google Drive Api_Google Apis Explorer - Fatal编程技术网

Google api 作为字符串返回的文件

Google api 作为字符串返回的文件,google-api,google-drive-api,google-apis-explorer,Google Api,Google Drive Api,Google Apis Explorer,尝试使用javascript和此处引用的google file api下载文件时: 响应主体已编码为文本。问题是下载二进制文件.sav,然后这些文件的编码不正确。我使用的代码是直截了当的: gapi.client.drive.files.get({ 'fileId': self.selectedFileId, alt: 'media' }).then((response) => { // response.body is text } 根据,您可以将encoding设置为null,但在

尝试使用javascript和此处引用的google file api下载文件时:

响应主体已编码为文本。问题是下载二进制文件.sav,然后这些文件的编码不正确。我使用的代码是直截了当的:

gapi.client.drive.files.get({ 'fileId': self.selectedFileId, alt: 'media' }).then((response) => {
 // response.body is text
}

根据,您可以将encoding设置为null,但在当前的v3 REST api中我看不到任何地方。

好的,所以我发现我做错了什么,我将字符串直接转换为blob以传递到后端,在转换为blob之前,我需要转换为类型化数组:

      gapi.client.drive.files.get({ 'fileId': self.selectedFileId, alt: 'media' }).then((response) => {

          const charArray = new Array(response.body.length);
          for (let i = 0; i < response.body.length; i++) {
              charArray[i] = response.body.charCodeAt(i);
          }
          const typedArray = new Uint8Array(charArray);

          const blob = new Blob([typedArray], {type: response.headers['Content-Type']});

希望这对某人有所帮助。

好吧,我知道我做错了什么,我把字符串直接转换成blob以传递到后端,在转换成blob之前,我需要转换成类型化数组:

      gapi.client.drive.files.get({ 'fileId': self.selectedFileId, alt: 'media' }).then((response) => {

          const charArray = new Array(response.body.length);
          for (let i = 0; i < response.body.length; i++) {
              charArray[i] = response.body.charCodeAt(i);
          }
          const typedArray = new Uint8Array(charArray);

          const blob = new Blob([typedArray], {type: response.headers['Content-Type']});

希望这对某人有所帮助。

我遇到了完全相同的问题,驱动器上有一个jpeg文件,它返回一个字符串:u255;Øÿa\u0000\u0010JFIF\u0000\u0001\u0001\u0001\u0000\u0000

查看响应标题:

"headers": {
"cache-control": "private, max-age=0, must-revalidate",
"content-encoding": "gzip",
"content-type": "text/plain;charset=UTF-8",
"date": "Wed, 24 Feb 2021 20:57:27 GMT",
"expires": "Wed, 24 Feb 2021 20:57:27 GMT",
"server": "UploadServer",
"vary": "Origin, X-Origin",
"x-guploader-uploadid": "ABg5-Uxki1DGxu2guatOLHR3Ixp4HRPxPUvGVk0omTun3qGM_89kO4jYQl9OWhEBpQZW1gUphzmr1mdBtzgKUiOdUgabVBo30A",
"Content-Type": "image/jpeg"
},

您将看到有两个内容类型标题。。。。其中之一就是

"text/plain;charset=UTF-8"
我怀疑这是一个错误!毕竟,如果我使用alt:'media'以二进制文件的形式请求文件,我希望二进制文件不会被破坏,而二进制文件会被当作文本


多亏了@Dave3of5,它现在可以工作了

我遇到了完全相同的问题,驱动器上有一个jpeg文件,它返回一个字符串:u0000\u0010JFIF\u0000\u0001\u0001\u0001\u0000\u0000

查看响应标题:

"headers": {
"cache-control": "private, max-age=0, must-revalidate",
"content-encoding": "gzip",
"content-type": "text/plain;charset=UTF-8",
"date": "Wed, 24 Feb 2021 20:57:27 GMT",
"expires": "Wed, 24 Feb 2021 20:57:27 GMT",
"server": "UploadServer",
"vary": "Origin, X-Origin",
"x-guploader-uploadid": "ABg5-Uxki1DGxu2guatOLHR3Ixp4HRPxPUvGVk0omTun3qGM_89kO4jYQl9OWhEBpQZW1gUphzmr1mdBtzgKUiOdUgabVBo30A",
"Content-Type": "image/jpeg"
},

您将看到有两个内容类型标题。。。。其中之一就是

"text/plain;charset=UTF-8"
我怀疑这是一个错误!毕竟,如果我使用alt:'media'以二进制文件的形式请求文件,我希望二进制文件不会被破坏,而二进制文件会被当作文本


多亏了@Dave3of5,它现在可以工作了

您使用的是哪些示波器?您是否具有该文件的完全读取权限?不是元数据读取访问范围:是的,我拥有对该文件的完全访问权限。文件内容设置为后退,但它们被编码为string@Jescanellas不确定这是否有用?您正在使用哪些作用域?您是否具有该文件的完全读取权限?不是元数据读取访问范围:是的,我拥有对该文件的完全访问权限。文件内容设置为后退,但它们被编码为string@Jescanellas不确定这是否有用?