我正在撰写关于Google Drive API的新问题 .

我正在尝试将Base64 PDF文件上传到我的Google Cloud 端硬盘上的文件夹 . 我已成功设置我的APP密钥和所有其他API选项,我可以阅读Google Cloud 端硬盘文件夹上的文件列表,如果它不存在我可以创建一个新文件夹,但我无法上传PDF文件,我不知道为什么!

首先尝试

根据V3 API文档,我试图这样做(我正在使用AngularJS):

gapi.client.drive.files.create({
    resource: {
        'name': 'My first PDF.pdf',
        'mimeType': 'application/pdf',
        'parents': [$folderID]
    },
    media: {
        'mimeType': 'application/pdf',
        'body': $file
    },
    fields: 'id'
})
.then(function($res){
    console.log('The Res', $res);
});

$res 对象中,有新文件的ID,但新文件创建时没有名称(无 Headers ),并且为空(0Kb) . 如果我从Object中删除 media 键,将使用正确的名称和mimeType创建文件,但显然它没有任何内容 .

第二次尝试

使用此代码从GitHub https://github.com/googledrive/web-quickeditor/blob/master/src/components/drive/multipart.js上的Google Cloud 端硬盘示例中浏览Google Developers Docs i 've found another Methods, using Multipart (i') . 根据这个,我试过这样做:

var $metadata = {
    'name': 'My first PDF.pdf',
    'mimeType': 'application/pdf',
    'parents': [$folderID]
};
var $multipart = new MultiPartBuilder()
    .append('application/json', JSON.stringify($metadata))
    .append($mimeType, $file)
    .finish();
var $request = gapi.client.request({
    path: '/upload/drive/v3/files',
    method: 'POST',
    params: {
        uploadType: 'multipart',
        fields: 'id'
    },
    headers: {'Content-Type' : $multipart.type},
    body: $multipart.body
});
$request().then(function($res){
    console.log($res);
});

同样在这种情况下,我能够创建文件,具有正确的文件名和在正确的文件夹中,但一如既往,文件si空!在一种情况下,文件似乎已满(72Kb的数据),但打开它将导致损坏的文件错误 .

有人能帮助我吗?

非常感谢你!