![](/img/trans.png)
[英]How to restore a JSON string from a compressed blob in Google Apps Script?
[英]How to reconstruct a blob for an image from a binary string (client side hidden form field) in Google Apps Script
我正在尝试使用Google HTMLService从客户端处理表单。 棘手的部分是,我想捕获表单中的粘贴图像(从剪贴板),将其发送到服务器端并将其保存在DriveApp中。
在客户端,我认为我能够基于此页面捕获粘贴的图像: http : //joelb.me/blog/2011/code-snippet-accessing-clipboard-images-with-javascript/
我的客户端代码如下所示:
function pasteHandler(e) {
// We need to check if event.clipboardData is supported (Chrome)
if (e.clipboardData) {
// Get the items from the clipboard
var items = e.clipboardData.items;
if (items) {
// Loop through all items, looking for any kind of image
for (var i = 0; i < items.length; i++) {
if (items[i].type.indexOf("image") !== -1) {
// We need to represent the image as a file,
var blob = items[i].getAsFile();
// and use a URL or webkitURL (whichever is available to the browser)
// to create a temporary URL to the object
var URLObj = window.URL || window.webkitURL;
var source = URLObj.createObjectURL(blob);
var reader = new FileReader();
reader.onload = function(e) {
document.getElementById("summaryImage").value= reader.result;
}
reader.readAsBinaryString(blob);
// The URL can then be used as the source of an image
createImage(source);
}
}
}
// If we can't handle clipboard data directly (Firefox),
// we need to read what was pasted from the contenteditable element
}
}
因此,更改只是我使用FileReader读取blob并将其保存到表单的隐藏字段中。
<input type="hidden" name="summaryImage" id='summaryImage' value=''>
我认为这部分工作正常,因为我可以在客户端显示图像,并且隐藏字段也填充有字节流。
我不确定的是如何获取隐藏字段的值(现在是“二进制字符串”)并将其另存为Google Drive中的适当PNG文件和/或查看它。
在服务器端,我能够保存任何上载的文件(使用输入文件表单元素),但请注意隐藏字段传递的二进制字符串。
我已经尝试过以下方法:
blob = form.summaryImage;
DriveApp.createFile('TRIFILE2.png', blob, 'image/png');
这确实会创建一个文件,但不能将其视为png。 如果我在Google云端硬盘中打开它,则显示如下。 我想它知道这是一个PNG文件,但是转换它的正确方法是什么?
‰PNG
IHDR y 2 Þ R IDATx í]mLTYš~Øl€I ÃF ´šX ‰|Dc H(œ TÿàcÒÅPkuÂGÿ°è B'*ì: èHÖ =ÂàØà¸É”ýc¨ùa &–M75i„ ]0ÓR&’¢W£`RZ› ÅA·
LÒ`²¹›s?ªîïÂ[x©{ªÛÔóñž÷}žûž{îåT=i×JJ ÐWJ# Æ\ %9¥) þ!Åã£á ’¬Š“€f²
h¦$S’U€€
B¤™LIV * ‘f2%Y ¨ DšÉ”d ‚ i&S’U€€
B¤™LIV * ‘f2%Y ¨ DšÉ”d ‚ i&S’U€€
B¤™LIV * ‘f2%Y ¨ DšÉ”d ‚ i&S’U€€
B¤™LIV * ‘f2%Y ¨ DšÉ”d ‚ i&S’U€€
B¤™LIV * 1 a ú;^)N4 ®Sœ` %™’¬ T "ÍdJ²
PAˆ4“)É*@@ !ÒL¦$« „H3™’¬ T "ÍdJ²
PAˆ4“)É*@@ !ÒL¦$« „H3™’¬ T "ÍdJ²
PAˆ4“)É*@@ !ÒL¦$‹ pc -
我也尝试了Utilities.newBlob()函数,但无法弄清楚如何从二进制字符串中创建正确的Blob。
不要使用readAsBinaryString
,而要使用readAsDataURL
,它会获得一个base64,而您不会感到一团糟。
如果您需要服务器端代码,则可以在此处遵循我的代码和Sandy Good教程:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.