如何在Javascript(Google Drive API)中保存HTTP请求中的URI
How to save URI from HTTP request in Javascript (Google Drive API)
我拼命地试图遵循这个:https://developers.google.com/drive/v3/web/manage-uploads#save-session-uri
我已经能够使用 javascript 将文件上传到我的 Google 云端硬盘,但是我现在正在尝试上传大文件 (>1GB),这会使我当前脚本中的浏览器崩溃。所以我的新脚本使用可恢复上传选项。我已发送可恢复会话启动请求,并收到带有位置标头 URI 的 200 OK 标头。
然后,本教程说"复制并保存会话 URI,以便您可以将其用于后续请求。我一辈子都无法弄清楚如何在 javascript 中做到这一点?如何从标头保存 URI?我这样做的方式是错误的吗?我更习惯于python(我已经设法让可恢复的上传工作),但遗憾的是我们需要在javascript中完成这项工作。我的代码(显然删除了我的客户端ID):
// Your Client ID can be retrieved from your project in the Google
// Developer Console, https://console.developers.google.com
var CLIENT_ID = '<YOURCLIENTID>';
var SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly', 'https://www.googleapis.com/auth/drive.file'];
/**
* Check if current user has authorized this application.
*/
function checkAuth() {
gapi.auth.authorize(
{
'client_id': CLIENT_ID,
'scope': SCOPES.join(' '),
'immediate': true
}, handleAuthResult);
}
/**
* Handle response from authorization server.
* @param {Object} authResult Authorization result.
*/
function handleAuthResult(authResult) {
var authorizeDiv = document.getElementById('authorize-div');
if (authResult && !authResult.error) {
// Hide auth UI, then load client library.
authorizeDiv.style.display = 'none';
loadDriveApi();
} else {
// Show auth UI, allowing the user to initiate authorization by
// clicking authorize button.
authorizeDiv.style.display = 'inline';
}
}
/**
* Initiate auth flow in response to user clicking authorize button.
* @param {Event} event Button click event.
*/
function handleAuthClick(event) {
gapi.auth.authorize(
{client_id: CLIENT_ID, scope: SCOPES, immediate: false},
handleAuthResult);
return false;
}
/**
* Load Drive API client library.
* Use last parameter (empty function) to do something on load!
*/
function loadDriveApi() {
gapi.client.load('drive', 'v3', function(){});
}
function createFile(fileData) {
var contentType = fileData.type || 'application/octet-stream';
alert(fileData.name + " " + contentType);
// Metadata
var metadata = {
'name' : fileData.name,
'mimeType': contentType,
'parents': [{'id':'0B1c3-viP2d_8QWMyenczTzdzSkk'}]
};
var request = gapi.client.request({
'path' : 'upload/drive/v3/files',
'method' : 'POST',
'params' : {'uploadType':'resumable'},
'headers' : {
'X-Upload-Content-Type' : contentType,
'X-Upload-Content-Length' : 1024*256
},
'body' : metadata
});
request.execute();
}
/**
* Append a pre element to the body containing the given message as its text node.
* @param {string} message Text to be placed in pre element.
*/
function appendPre(message) {
var pre = document.getElementById('output');
var textContent = document.createTextNode(message + ''n');
pre.appendChild(textContent);
}
//files is a filelist
function fileselected(files)
{
for(var i = 0; i < files.length; i++) // for file in list of files
{
var f = files[i]; // Pick the current file (i) from the file list
appendPre("Uploading " + f.name);
createFile(f);
appendPre(f.name + " upload complete"); // Upload the current file
}
}
正如 OP 最初在编辑问题时所说的那样,您需要像这样使用 request.execute()
的回调:
request.execute(function(resp, raw_resp) {
appendPre(raw_resp);
// GETS THE LOCATION! WOO
appendPre(JSON.parse(raw_resp).gapiRequest.data.headers.location);
});
相关文章:
- Meteor如何接收HTTP请求
- 如何将getJson的响应保存在全局变量中
- 按下按钮时保存cookie
- 如何使用 Angular JS 将数据保存在数据库中
- 子字符串/正则表达式以获取字符串中保存的 SRC 值
- HTML5在画布中加载较小的图像并保存实际大小的图像
- 是否可以在浏览器中使用纯JavaScript保存音频流
- Firefox插件:保存到文本文件的HTTP日志,其中包含时间、用户名、计算机名;t创建文本文件
- 在firefox扩展上观察并保存http请求
- React Native 将 http 图像保存到 Android 相机胶卷
- 如何在Javascript(Google Drive API)中保存HTTP请求中的URI
- AngularJS$http.post成功,但没有't保存数据
- 从视图中调用MVC控制器中的HTTP POST方法'数据库保存
- 可以't将http结果保存到作用域
- Node.js -使用http.get循环遍历要保存在本地的JSON文件列表
- HTTP头使浏览器显示"save as"对话框并保存文件
- 可接受将要求(fs/http/等)保存在子模块的全局命名空间中
- nodejs如何在http服务器中保存响应
- 将javascript http响应保存到变量时遇到问题
- 主干模型被保存,但向HTTP发送一个空数组