-1

Trying to created a Google Apps Script to prompt me to choose a file to upload to Google Drive as described here

But I am receiving an error

Uncaught ScriptError: TypeError: Cannot read property 'getAs' of undefined

I have watched the YouTube video a couple of times now, but I don't see that I am doing anything incorrectly. Any ideas?

function doGet(){
  return HtmlService.createHtmlOutputFromFile("form.html")
}

function upload(e){
  // logic to upload the file
  var destination_id = 'enter your folder id here' //folder id
  var img = e.imageFile
  var contentType = 'img/png'
  var destination = DriveApp.getFolderById(destination_id)
  var img = img.getAs(contentType)
  destination.createFile(img)
  
}
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <form>
    <input type="file" name="imageFile"/>
    <input type="button" value="Upload" onclick="google.script.run.upload(this.parentNode)">
  
    </form>
    
  </body>
</html>
Rubén
  • 34,714
  • 9
  • 70
  • 166
  • 1
    I think that the reason of the error message can be resolve by [Rubén's answer](https://stackoverflow.com/a/63784262/7108653). But in your script, if you use V8 runtime, as the additional issue, the binary file cannot be directly send from HTML side to Google Apps Script side. So I think that this thread might also answer for the additional issue. https://stackoverflow.com/q/41994351 – Tanaike Sep 07 '20 at 22:03
  • I suggest you to add more details to your question including name and version of the operative system and web brower used. Also, I suggest you to try using your web browser in incognito/private/safe mode with all the extensions disabled. – Rubén Sep 07 '20 at 22:28
  • @Tanaike Still doesn't explain `Uncaught ScriptError: TypeError: Cannot read property 'getAs' of undefined` – TheMaster Sep 08 '20 at 02:07
  • @Winston Where and when do you get this error? Is it in view> executions? Or somewhere else. What was the last thing you did before this error is thrown? – TheMaster Sep 08 '20 at 02:09
  • The version can be found in view>show manifest file>appscript.json – TheMaster Sep 08 '20 at 02:10
  • 2
    @TheMaster Thank you for your comment. If my understanding is correct, in that case, I think that `img` might be `undefined`. But in Rubén's answer, it says `Regarding the specific error that you got, perphaps you didn't publish the code shown in the question as a new version of your web application. Try using the link to test the latest code.`. So I didn't mention about it as the additional information. I think that when OP showed the current situation as the additional information, we can think of it. – Tanaike Sep 08 '20 at 02:28

1 Answers1

-1

The code has an error

replace

var contentType = 'img/png'

by

var contentType = 'image/png'

Applyng the above fix, the code worked fine for me.

NOTE: While code doesn't throw any error using the default runtime (Chrome V8), the file was not correctly uploaded but using the old runtine (Mozilla Rhino) it works fine.

Regarding the specific error that you got, perphaps you didn't publish the code shown in the question as a new version of your web application. Try using the link to test the latest code.

Related

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Thanks. I replaced the double quotes with single quotes. I then clicked on Save icon. I then Deployed as Web App. I chnaged Project version to New, and entered some comments as to the changee and clicked update. I copied the URL into a new browser window. My new error message: >Uncaught ScriptError: TypeError: Cannot read property 'getAs' of undefined – Winston Snyder Sep 07 '20 at 21:43
  • 1
    I think that OP's script might have 2 important issues. One issue can be resolved by your answer. As another issue, if OP uses V8, when the image file is created with `e.imageFile`, the file is broken because of the issue of the character code. [Ref](https://stackoverflow.com/q/41994351) So when you add more information about this, I think that it will be useful for OP and other users. – Tanaike Sep 07 '20 at 22:13
  • 1
    Thank you for replying and the information. From your replying, finary, that bug might be resolved at Google side. I had confirmed that above flow cannot be used 2 days ago. I would like to confirm it soon. – Tanaike Sep 07 '20 at 22:22
  • 1
    When I confirm it, I confirmed that the binary file using OP's script including your modification is uploaded, the created image file cannot be opened. So in my environment, it seems that the bug is still not resolved. When the text file is used, the data can be retrieved from `e.imageFile`. How about this? – Tanaike Sep 07 '20 at 22:31
  • @Ruben How do I find the version of Google Apps Script (GAS) I am using? I tried searching, but all I am getting back is about version control in GAS. I'm using Firefox 78.2.0esr. – Winston Snyder Sep 07 '20 at 22:43
  • @WinstonSnyder Google doesn't publish the Google Apps Script version, so don't worry for it. What operative system are you using? – Rubén Sep 07 '20 at 22:47
  • 1
    @Tanaike You are right using `` on [tag:google-apps-script]+[tag:V8] the file is uploaded but it's corrupted. Using the old runtime works fine. – Rubén Sep 08 '20 at 00:15
  • 1
    @Rubén Thank you for replying and testing it. Yes. By this issue, I added one more sample script for V8 runtime to there. I think that at V8, when the binary data is included in `this.parentNode`, the data is broken at Google Apps Script because of the issue of the character code. I believe that this issue will be resolved in the future update. As a simple sample, I have also published the script for sending the various values with the form submission to Google Apps Script side. https://gist.github.com/tanaikech/58d96c023468fc1922d67764251b25e0 – Tanaike Sep 08 '20 at 00:22
  • @Ruben I'm using Windows 10. – Winston Snyder Sep 08 '20 at 05:20