Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 谷歌脚本base64解码并将文件保存到驱动器_Google Apps Script_Google Drive Api_Base64_Google Sheets Api - Fatal编程技术网

Google apps script 谷歌脚本base64解码并将文件保存到驱动器

Google apps script 谷歌脚本base64解码并将文件保存到驱动器,google-apps-script,google-drive-api,base64,google-sheets-api,Google Apps Script,Google Drive Api,Base64,Google Sheets Api,我收到了一张Google Sheets脚本中的Base64编码图像。我可以通过日志成功生成图像 e.postData.contents 然后使用来解码并生成一个.jpg文件。但是当我试图在google脚本中进行解码并编写一个jpg文件来驱动时,它被破坏了。看起来很相似,但插入了一些替换字符。这是我的密码: function doPost(e) { var decoded = Utilities.base64Decode(e.postData.contents, Utilities.Char

我收到了一张Google Sheets脚本中的Base64编码图像。我可以通过日志成功生成图像

e.postData.contents
然后使用来解码并生成一个.jpg文件。但是当我试图在google脚本中进行解码并编写一个jpg文件来驱动时,它被破坏了。看起来很相似,但插入了一些替换字符。这是我的密码:

function doPost(e) {
  var decoded = Utilities.base64Decode(e.postData.contents, Utilities.Charset.UTF_8);
  var blob = Utilities.newBlob(decoded);
  DriveApp.createFile('img_'+date+'.jpg', blob.getDataAsString('Windows-1252'), MimeType.JPEG);
}
我还尝试了
blob.getAs('image/jpeg')
,但它只返回一个包含文本
blob
的4字节文件


有什么想法吗?我尝试了一些不同的字符集,而
Windows-1252
似乎给出了最接近的结果。

这个示例脚本怎么样?使用此选项时,请定义日期。在我的测试中,我还可以确认base64编码的jpeg文件可以在
https://www.base64decode.org/

更新
doPost()
时,请注意以下几点。请将脚本作为新版本重新部署到Web应用。如果未执行此操作,则不会更新已部署Web应用的脚本。

示例脚本: 卷曲样本: 如果您想使用curl尝试使用
doPost()
,可以使用以下命令。当您使用此文件时,请输入文件名,即base64编码的文件和Web应用程序URL

curl -L -F "contents=`cat ### Filename ###`" "https://script.google.com/macros/s/#####/exec"

如果我误解了你的问题,我很抱歉。

很好,谢谢塔奈克!所以我猜我的原始代码不起作用的原因是
newBlob
需要知道
contentType
@James B Welcome。也谢谢你。
curl -L -F "contents=`cat ### Filename ###`" "https://script.google.com/macros/s/#####/exec"