首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用原始XMLHttpRequest批量删除Google上的文件

用原始XMLHttpRequest批量删除Google上的文件
EN

Stack Overflow用户
提问于 2017-05-23 18:38:19
回答 1查看 723关注 0票数 0

我正在使用这个在镀铬扩展中。

带有文件ID的简单请求将删除单个文件

代码语言:javascript
复制
xhr.open('DELETE', 'https://www.googleapis.com/drive/v2/files/' + ID, true);

如果我想从Google删除多个文件,我会循环ID的数组并发送相同的请求,只是多次。

大多数请求都成功了,但是如果我有超过7-8的请求,其中一些请求会在error code 403中失败(我认为这是禁止的)。

通常,如果我有12个文件要删除,两个或三个将失败。

(当我重复请求时,它们将被删除)

Google是否有一些防止节流的保护措施,以及如何删除多个文件?

用计时器拖延(如)。( 100 ms)是不可取的,因为我可以删除数百个文件,处理它需要10-30秒。

REST驱动器API文档没有提到删除多个文件,只有一个文件

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-16 20:57:43

这是使用REST从Google删除文件的批处理请求的示例代码:

代码语言:javascript
复制
var arrayOfFileIds;  // array of id's of the files you want to delete
//notice that currently you can only batch up to 100 requests.
var authToken;  //your OAuth2 token.
var xhr = new XMLHttpRequest;
var boundary = "END_OF_PART";
var separation = "\n--"+boundary + "\n";
var ending = "\n--" + boundary + "--";

var requestBody = arrayOfFileIds.reduce((accum,current)=>{
           accum += separation +
           "Content-Type: application/http\n\n" +
           "DELETE https://www.googleapis.com/drive/v2/files/" +
           current +
           "\nAuthorization: Bearer " + authToken;
           return accum;
         },"") + ending;

xhr.onload = ()=>{
    console.log(xhr.response);
    //handle the response
};
xhr.open("POST", "https://www.googleapis.com/batch/drive/v2", true);
xhr.setRequestHeader("Content-Type","multipart/mixed; boundary=" + boundary);
xhr.send(requestBody);

可选地,您可以向每个请求(在上面的content-id中)添加一个requestBody头,以标识批处理响应中的各个响应。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44142611

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档