title: Google App Script to Upload File from Website to Google Drive - works for text file but not binary
body: <p>I need to upload files to my Google Drive via a Google App Script. The goal is to be able to let users upload a file to my Google Drive from my website. The code is largely the same as a tutorial I found online, I just added a link to my website banner and also added some code to send me an email when someone uploads a file.</p>
  <p>The problem is, it works for text file but not binary files (jpeg, pdf, etc). It correctly creates a file in the appropriate folder, but the file cannot be opened. The binary file that gets created does have some data in it.. when I open the created file in TextEdit, it does look somewhat similar to the original, but some of the characters look different. The filesize of the new created file is also larger than the original.</p>
  <p>The problem does not occur with text files. The code is listed below.</p>
  <p>HTML code</p>
  <pre><code>&lt;!doctype html&gt;
  &lt;style type=&quot;text/css&quot;&gt;
  body {
      background-color: #FFFFFF;
  }
  &lt;/style&gt;
  &lt;BR&gt;
  &lt;BR&gt;
  &lt;BR&gt;
  &lt;div align=&quot;center&quot;&gt;
    &lt;p&gt;&lt;img src=&quot;https://rosspalmermd.files.wordpress.com/2020/05/color-logo-with-background.png&quot; width=320&gt;&lt;/p&gt;
    &lt;table width=&quot;459&quot; border=&quot;0&quot;&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td width=&quot;462&quot;&gt;&lt;div align=&quot;center&quot;&gt;
            &lt;hr&gt;
          &lt;/div&gt;
            &lt;form id=&quot;myForm&quot; align=&quot;center&quot;&gt;
              &lt;input type=&quot;text&quot; name=&quot;myName&quot; placeholder=&quot;Your name...&quot;&gt;
              &lt;input type=&quot;email&quot; name=&quot;myEmail&quot; placeholder=&quot;Your email...&quot;&gt;
              &lt;input type=&quot;file&quot; name=&quot;myFile&quot;&gt;
              &lt;input type=&quot;submit&quot; value=&quot;Upload File&quot; 
             onclick=&quot;this.value='Uploading..';
                      google.script.run.withSuccessHandler(fileUploaded)
                      .uploadFiles(this.parentNode);
                      return false;&quot;&gt;
            &lt;/form&gt;
            &lt;div id=&quot;output&quot;&gt;&lt;/div&gt;
            &lt;script&gt;
      function fileUploaded(status) {
          document.getElementById('myForm').style.display = 'none';
          document.getElementById('output').innerHTML = status;
      }
            &lt;/script&gt;
          &lt;style&gt;
   input { display:block; margin: 20px; }
   
            &lt;/style&gt;
          &lt;hr&gt;&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
    &lt;h3&gt;&amp;nbsp;&lt;/h3&gt;
    &lt;p&gt;&amp;nbsp;&lt;/p&gt;
  &lt;/div&gt;
  </code></pre>
  <p>Server side code:</p>
  <pre><code>
  
  function doGet(e) {
    return HtmlService.createHtmlOutputFromFile('form.html');
          
  }
  
  function uploadFiles(form) {
    
    try {
      
      var dropbox = &quot;DriveUploader&quot;;
      var folder, folders = DriveApp.getFoldersByName(dropbox);
      
      if (folders.hasNext()) {
        folder = folders.next();
      } else {
        folder = DriveApp.createFolder(dropbox);
      }
      
      var blob = form.myFile; 
      var file = folder.createFile(blob); 
      
      file.setDescription(&quot;Uploaded by &quot; + form.myName);
          
      MailApp.sendEmail(&quot;email@email.com&quot;,
                    &quot;File uploaded to website&quot;,
                    form.myName + &quot; (&quot; + form.myEmail + &quot;) has uploaded a file &quot; + file.getUrl());
      
      return &quot;File uploaded successfully. We will contact you soon.&quot;;
      
    } catch (error) {
      
      return error.toString();
    }
  }
  </code></pre>