获取外部 URL 并保存到云端硬盘
Get external URL and save to Drive
我想获取 PDF URL 并将其保存到云端硬盘。文件夹 ID 和 URL 正确。
function uploadDoc(){
var folder = DriveApp.getFolderById('my_folder_id');
var url = 'my_url';
var blob = UrlFetchApp.fetch(url);
var blob2 = blob.getAs('application/pdf');
var newFile = folder.createFile('filename.pdf',blob2);
}
当我 运行 这个时,我得到:
Exception: Converting from binary/octet-stream to application/pdf is not supported.
您需要解决两件事:
UrlFetchApp.fetch(url)
后需要使用getBlob()
method。 getAs('application/pdf')
方法不是严格要求的。
如果您使用的是 blob,则需要使用 createFile(blob)
method.
您正在使用 createFile(name, content)
method 并且在此方法中,content
是字符串而不是 blob。
然后到新文件的名称,你需要 setName(name)
method.
function uploadDoc() {
var url = 'https://www.gstatic.com/covid19/mobility/2021-02-21_AF_Mobility_Report_en.pdf';
var foID = 'my_folder_id';
var folder = DriveApp.getFolderById(foID);
var blob = UrlFetchApp.fetch(url).getBlob();
var newFile = folder.createFile(blob).setName('filename.pdf');
}
我想获取 PDF URL 并将其保存到云端硬盘。文件夹 ID 和 URL 正确。
function uploadDoc(){
var folder = DriveApp.getFolderById('my_folder_id');
var url = 'my_url';
var blob = UrlFetchApp.fetch(url);
var blob2 = blob.getAs('application/pdf');
var newFile = folder.createFile('filename.pdf',blob2);
}
当我 运行 这个时,我得到:
Exception: Converting from binary/octet-stream to application/pdf is not supported.
您需要解决两件事:
UrlFetchApp.fetch(url)
后需要使用getBlob()
method。getAs('application/pdf')
方法不是严格要求的。如果您使用的是 blob,则需要使用
createFile(blob)
method.
您正在使用 createFile(name, content)
method 并且在此方法中,content
是字符串而不是 blob。
然后到新文件的名称,你需要 setName(name)
method.
function uploadDoc() {
var url = 'https://www.gstatic.com/covid19/mobility/2021-02-21_AF_Mobility_Report_en.pdf';
var foID = 'my_folder_id';
var folder = DriveApp.getFolderById(foID);
var blob = UrlFetchApp.fetch(url).getBlob();
var newFile = folder.createFile(blob).setName('filename.pdf');
}