当我调用DriveApp.createFile(image)时,保存的图像已损坏

时间:2018-06-27 16:46:41

标签: google-apps-script google-drive-api

尝试打开它时出现错误“无可用预览”。我正在尝试从外部网站获取图片。

这是我的代码尝试:

var contentType = "image/jpeg";
var options = {
    "method" : "get",
    "contentType": contentType,
    'muteHttpExceptions': true
};

var url = "www.externalwebsite.com/12344.jpg";
var response = UrlFetchApp.fetch(url, options);
Logger.log(response.getContentText());
var blob = response.getBlob();
blob.setContentTypeFromExtension();

Logger.log(blob.getContentType());
var image = blob.getAs(contentType);

var file = DriveApp.createFile("file1.jpg",blob,contentType);

var file2 = DriveApp.createFile(Utilities.newBlob(blob, MimeType.JPEG).getAs(MimeType.JPEG).setName('file2.jpg'));

var file3 = DriveApp.createFile(blob);

var data = Utilities.base64Decode(blob, Utilities.Charset.UTF_8);
var blobFile = Utilities.newBlob(data, MimeType.JPEG, "file4.jpg");
     DriveApp.createFile(blobFile);

也尝试使用高级驱动器:

var file5 = {
    title: 'file5.jpg',
    mimeType: 'image/jpeg'
};
file = Drive.Files.insert(file5, blob);

0 个答案:

没有答案