更新存储在Google Drive上的文件的内容
updating the content of a file stored on Google Drive
我试图使用javascript将存储在Google Drive上的文件(例如.txt或.java文件)的内容设置为特定的String。
我知道更新存储在Google Drive上的文件的正确方法是
function updateFile(fileId, fileMetadata, fileData, callback) {
const boundary = '-------314159265358979323846';
const delimiter = "'r'n--" + boundary + "'r'n";
const close_delim = "'r'n--" + boundary + "--";
//console.log("fileId: "+fileId+" fileData: "+fileData);
var reader = new FileReader();
reader.readAsBinaryString(fileData);
reader.onload = function(e) {
console.log("reader initialized");
var contentType = fileData.type || 'application/octet-stream';
// Updating the metadata is optional and you can instead use the value from drive.files.get.
var base64Data = btoa(reader.result);
var multipartRequestBody =
delimiter +
'Content-Type: application/json'r'n'r'n' +
JSON.stringify(fileMetadata) +
delimiter +
'Content-Type: ' + contentType + ''r'n' +
'Content-Transfer-Encoding: base64'r'n' +
''r'n' +
base64Data +
close_delim;
var request = gapi.client.request({
'path': '/upload/drive/v2/files/' + fileId,
'method': 'PUT',
'params': {'uploadType': 'multipart', 'alt': 'json'},
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
},
'body': multipartRequestBody});
if (!callback) {
callback = function(file) {
console.log(file)
};
}
request.execute(callback);
}
}
但这在将内容设置为特定字符串时没有起到作用。
没关系。我找到了问题的答案。使用我的问题中的方法updateFile
:
function save(fileId, content){
var contentArray = new Array(content.length);
for (var i = 0; i < contentArray.length; i++) {
contentArray[i] = content.charCodeAt(i);
}
var byteArray = new Uint8Array(contentArray);
var blob = new Blob([byteArray], {type: 'text/plain'});
var request = gapi.client.drive.files.get({'fileId': fileId});
request.execute(function(resp) {
updateFile(fileId,resp,blob,changesSaved);
// the callback^^^^^^
});
}
相关文章:
- 无法使用API-node.js从Google Drive下载文件
- 使用JavaScript的Google Drive:处理文件内容
- 如何使用PHP从我自己的网站上的Google Drive获取文件和文件夹列表
- Google Drive是否不再允许从其他网站引用JavaScript文件
- 如何从Chrome扩展下载公共Google Drive文件
- 使用Google Drive Viewer显示通过Google Drive API创建的文件
- 从Google Drive获取文件并隐藏到Blob
- 更新存储在Google Drive上的文件的内容
- 使用 Google Drive API v3 (javascript) 创建文件
- 无需验证即可访问共享Google Drive文件夹中的文件,JavaScript
- Google Drive Web API”;文件:列表;带有q参数和nextPageToken的命令无法正常工作
- 使用Google Drive SDK上传JSON文件是零字节(javascript)
- 谷歌应用程序编写Web应用程序脚本以在用户's的myDrive文件夹仅将文件发送到MY Drive(应用程序的创
- Google Drive API公共下载私人帐户文件
- Google Drive API 400分块文件上传错误请求
- 当尝试将文件上传到Google drive api时,出现404提示,怀疑是数据格式化问题
- 在Google Drive / Google Sheet中重命名文件
- Google Drive文件上传使用JavaScript API抛出X-Frame-Options错误
- Google Drive API;谷歌实时API -我可以把一个实时文件在应用程序文件夹
- 在Google Drive SDK中一次创建多个文件夹