2017-05-08 56 views
1

我想使用src属性在google apps脚本中显示图像。我把文件夹ID和路径。但图像无法显示。我在这里分享了文件夹。如何在谷歌应用程序脚本中使用<img src >

谢谢你的帮助!

https://drive.google.com/drive/folders/0ByNw-B9nXMcbMkxHTGt2X2xUTzQ

function doGet() { 
var html = HtmlService.createTemplateFromFile('index').evaluate() 
.setTitle('picture').setSandboxMode(HtmlService.SandboxMode.NATIVE); 
return html; 
} 

<!DOCTYPE html> 
<html> 
    <head> 
    <base target="_top"> 
    </head> 
    <body> 
    <p> Dispaly picture </p> 
    <p><img src="https://drive.google.com/drive/folders/0ByNw-B9nXMcbMkxHTGt2X2xUTzQ/200w.gif"> </p> 
    </body> 
</html> 

回答

4

img src=""要求你要显示的实际图像,不包含它的文件夹中的网址。要获得正确的链接,您需要在自己的窗口中打开图片或点击共享并获取链接。

正确的链接,你的形象是:

https://drive.google.com/uc?export=download&id=

https://drive.google.com/file/d/0ByNw-B9nXMcbSTN2S2ZiYWprdzA/view

要在src时,您需要将FILEID添加到这种格式URL的末尾使用此所以它变成:

https://drive.google.com/uc?export=download&id=0ByNw-B9nXMcbSTN2S2ZiYWprdzA

0

由于某些原因,James Donnellan接受的答案对我无效。当我发布该页面时,我可以看到图像,但其他人无法看到。 我检查了共享设置,并试图用“作为我执行”选项发布。与给定的链接也尝试过,相同的结果。

尽管上面提到的方法使用起来更加简单,但我想分享一下在别人遇到同样问题的情况下我的工作方式。我使用的图像转换为字节:

在应用程序脚本:

function loadImageBytes(){ 
var id = "your_drive_file_id" 
var bytes = DriveApp.getFileById(id).getBlob().getBytes(); 
return Utilities.base64Encode(bytes); 
} 

在Javascript中

google.script.run 
.withSuccessHandler(function(bytes){ showImage(bytes) }) 
.loadImageBytes(); 

function showImage(bytes){ 
document.getElementById("ItemPreview").src = "data:image/png;base64," + bytes; 
} 

希望这有助于任何人。

相关问题