0

Related to my earlier question How to reconstruct a blob for an image from a binary string (client side hidden form field) in Google Apps Script, I digged further and realize my critical question is how to create a file in Google Drive if I know exactly the value for each byte of the file?

The DriveApp.createFile function accepts blobs, but I am not sure where to start if I want to create a file with each byte as 0x89,0xFF,0x99,... ?

Community
  • 1
  • 1
Pan Yan
  • 1,622
  • 1
  • 18
  • 21

1 Answers1

1

I haven't tried this before, but check the Utilities.newBlob() function.

Perhaps you can avoid the issue entirely if you Base64 Encode your data in the form, then you can use Utilities.base64Decode() on it and pass it to newBlob().

Once you get a Blob you should be able to use DriveApp.createFile().

https://developers.google.com/apps-script/reference/utilities/utilities#newBlob(Byte)

https://developers.google.com/apps-script/reference/utilities/utilities#base64Decode(String)

Cameron Roberts
  • 7,127
  • 1
  • 20
  • 32
  • I've been trying both functions with no success. blob=Utilities.newBlob(Utilities.base64Decode(form.summaryImage)); blob.setName('A.PNG'); DriveApp.createFile(blob.getAs('image/png')); This creates a file with four letters "Blob" in it... Am I missing something? – Pan Yan Sep 22 '15 at 16:54
  • If you're getting a file with the word "Blob" in it then it is creating the file with a string representation of the Blob object. Not sure why that would be, have you tried without blob.getAs(), and just passed the blob? Maybe try the alternate createFile: createFile("A.PNG",blob,"image/png"); and don't call setName on the blob at all. – Cameron Roberts Sep 22 '15 at 16:59
  • I think the actual issue I have is to get the data in correct Base64 format. The answer to my original question http://stackoverflow.com/questions/32707715/how-to-reconstruct-a-blob-for-an-image-from-a-binary-string-client-side-hidden helped. Just need to encode the original data in correct Base64. But it's still not clear to me how to make a Byte[] or Base64 object from say a list of binary byte values.... – Pan Yan Sep 22 '15 at 20:42