0

I've been using this to upload images to google drive for over a year with no problems. But starting in February none of the jpeg uploaded images can be viewed by any browser. I suspect that something changed at google and I need to update the code. I've searched, but I found nothing about and changes.

   <!DOCTYPE html>
    <html>
       <head>
      <base target="_top">
    </head>
    <body>
   <script>
     function updateUrl(imageUrl) {
      var div = document.getElementById('output');
       div.innerHTML = imageUrl;
         }
        </script>

         <form>
   <label>INCLUIR IMAGEM</label><input type="file" accept="image/*" capture="camera" 
     name="imageFile"/>

    <!-- <input type="file" name="imageFile">-->
  <input type="button" value="Upload File" 
   onclick="google.script.run.withSuccessHandler(updateUrl).upload(this.parentNode)">
 </form><br/><br/>
 <div id="output"></div>
  </body>
   </html>


 code.GS
    function upload(e) { 

 // Folder ID of destination folder
 var destination_id = '1qghuk1-kShwsdfsdf8Ndfsdfsddffs';

var contentType = 'image/jpeg'; 

var imageUrl = e.imageFile;

var destination = DriveApp.getFolderById(destination_id);
var file = destination.createFile(imageUrl);
var imageUrl = Drive.Files.get(file.getId()).webContentLink;
// Logger.log(imageUrl)
var reformatUrl = imageUrl.replace("?", "?export=view&").replace("&export=download", "")

 //Logger.log(reformatUrl); 
  return reformatUrl;

   }
user2916405
  • 147
  • 1
  • 14
  • 1
    take a look at the answer to this question: https://stackoverflow.com/questions/60742695/moving-google-apps-script-to-v8-file-upload-stopped-working-from-sidebar – Cooper Jun 29 '20 at 15:07
  • @Cooper Thank you for that . I've adjusted my code as per the workaround suggested on the link you provided and it works. I'm just hoping that this will last. – user2916405 Jun 29 '20 at 17:16
  • 1
    It will last until we experience another change. That's why it's called software. – Cooper Jun 29 '20 at 17:22
  • @Cooper I thought that this is the good comment. – Tanaike Jun 30 '20 at 00:11

1 Answers1

0

These are the changes I've made to the code in order to make it work with V8 activated.

`<!DOCTYPE html>
  <html>
  <head>
    <base target="_top">
  </head>
 <body>
<h1>File Uploader</h1>
 <form>
<input type="file" name="myFile" id="file">
<br>
<input class="blue" type="button" id="submitBtn" value="Upload File" 
onclick="uploadthis(this.parentNode)">

</form>

<input type="button" value="Close" onclick="google.script.host.close()" />

<script>
/*
function uploadthis(fileForm){//code replaced

google.script.run
.uploadFiles(fileForm)
}
 */
function uploadthis(fileForm){
const file = fileForm.myFile.files[0];
const fr = new FileReader();
fr.onload = function(e) {
 const obj = {
  // filename: file.name,  // In your script, the filename is given at GAS side. So I 
 removed this.
  mimeType: file.type,
  bytes: [...new Int8Array(e.target.result)]
 };
 google.script.run.withSuccessHandler(getUrl).uploadFiles(obj);
  };
  fr.readAsArrayBuffer(file);
  }

  function getUrl(url){
 document.getElementById('UploadedFileURL').innerHTML = url;

 }



  </script>
  <span id="UploadedFileURL"></span>
</body>
</html>`

CODE.GS

`function uploadFiles(data){
  var destination_id = '1qggfdgtgrfdgdfgfgdf';

  var file = Utilities.newBlob(data.bytes, data.mimeType, +" - SomeName - ");  // 
  Modified
  var destination = DriveApp.getFolderById(destination_id);
  var createFile = destination.createFile(file);
   var imageUrl = Drive.Files.get(createFile.getId()).webContentLink;
  // Logger.log(imageUrl)
   var reformatUrl = imageUrl.replace("?", "? 
   export=view&").replace("&export=download", "")

   return reformatUrl;  // return the clickable url
  }`
  
user2916405
  • 147
  • 1
  • 14