4

I'm trying to upload a file to a Google Web App with doPost(e), as shown below.

function doPost(e) {
    var blob = e.parameter.myFile;
    DriveApp.createFile(blob);
}

A file is then POSTed to the web app at the client side. The POST has been proved to work correctly with on my own web server.

However the blob variable, in the code above, is not actually a Blob type, it is a string type. How does Google script engine prepare parameter e? Is there a way to make this work?

user2106007
  • 697
  • 1
  • 7
  • 11
  • Try using `Utilities.newBlob(string)` [https://developers.google.com/apps-script/reference/utilities/utilities#newblobdata_3](https://developers.google.com/apps-script/reference/utilities/utilities#newblobdata_3) – Alan Wells May 20 '16 at 23:51
  • 1
    Thanks Sandy. I tried that. But the file created is almost double the size of the original one. Here is the code: – user2106007 May 21 '16 at 00:39
  • 1
    But the contents of the uploaded file are not the same, and the size is much larger. I guess Google script engine did not preserve the original data? If I knew how it was prepared I might be able to do something to get the original contents. – user2106007 May 21 '16 at 00:53
  • Utilities.newBlob() must be used with the bytes for the blob, not a simple string. I don't know why doPost() gets only the file's name as a string and not the file itself... See here https://developers.google.com/apps-script/reference/utilities/utilities#newBlob(Byte) – Maycow Moura Feb 14 '17 at 02:33

1 Answers1

2

I got the same problem here. An workaround you can do is convert your file to a Base64 string using FileReader(), upload it, and convert it back on server-side before sending it to your Google Drive.

Here's how to encode your file and decode it server-side using Utilities API

Maycow Moura
  • 6,471
  • 2
  • 22
  • 19