![](/img/trans.png)
[英]encodeURIComponent and content-type: 'charset: utf-8'
[英]Unwanted “content-type: text/plain;charset=UTF-8” header in Google Drive API Response
我正在使用浏览器 GAPI 库从 Google Drive 请求一段二进制数据。 来自google服务器的响应总是有一个content-type: text/plain;charset=UTF-8
header,因此,浏览器总是将二进制数据解码为UTF-8字符串。
更重要的是,解码过程似乎为原始二进制数据添加了填充。 例如,UTF-8 解码后的 282 字节二进制变为 422 字节长。
有没有办法告诉谷歌 API 服务器更改内容类型 header?
或者有没有办法绕过响应主体的预处理并获取原始响应?
我的请求代码在这里列出:
currentApiRequest = {
path: `https://www.googleapis.com/drive/v3/files/${fileID}`,
params: {
alt: "media"
}
}
gapi.client.request(currentApiRequest).then(
(response) => {
let data = response.body;
console.log(byteSize(data));
console.log(data);
}
)
下面的修改呢? 在此修改中,首先将检索到的数据转换为 Unit8Array 并将其转换为 blob。
const fileID = "###"; // Please set your file ID.
currentApiRequest = {
path: `https://www.googleapis.com/drive/v3/files/${fileID}`,
params: {alt: "media"}
};
gapi.client.request(currentApiRequest)
.then((response) => {
let data = response.body;
const blob = new Blob([new Uint8Array(data.length).map((_, i) => data.charCodeAt(i))]);
// When you use the following script, you can confirm whether this blob can be used as the correct data.
const filename = "sample.png"; // Please set the sample filename.
const a = document.createElement('a');
document.body.appendChild(a);
a.href = URL.createObjectURL(blob);
a.download = filename;
a.click();
});
gapi.client.drive.files.get()
检索到的数据。 另一方面,在这个问题中,使用从gapi.client.request()
检索到的数据。 我认为这可能对其他用户也有用。 所以我发布了这个答案,没有标记为重复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.