assets/Code examples/WebAppSDK/photoSharingWidget/js/example3.js
/**
       * Insert new file.
       *
       * @param {File} fileData File object to read data from.
       * @param {Function} callback Function to call when the request is complete.
       */
Insert new file.
@param {File} fileData File object to read data from.
@param {Function} callback Function to call when the request is complete.
Insert new file . @param { File } fileData File object to read data from . @param { Function } callback Function to call when the request is complete .
function insertFile(fileData, callback) {
        const boundary = '-------314159265358979323846';
        const delimiter = "\r\n--" + boundary + "\r\n";
        const close_delim = "\r\n--" + boundary + "--";
        var reader = new FileReader();
        reader.readAsBinaryString(fileData);
        reader.onload = function(e) {
          var contentType = fileData.type || 'application/octet-stream';
          var metadata = {
            'title': fileData.name,
            'mimeType': contentType
          };
          var base64Data = btoa(reader.result);
          var multipartRequestBody =
              delimiter +
              'Content-Type: application/json\r\n\r\n' +
              JSON.stringify(metadata) +
              delimiter +
              'Content-Type: ' + contentType + '\r\n' +
              'Content-Transfer-Encoding: base64\r\n' +
              '\r\n' +
              base64Data +
              close_delim;
          var request = gapi.client.request({
              'path': '/upload/drive/v2/files',
              'method': 'POST',
              'params': {'uploadType': 'multipart','visibility': 'DEFAULT'},
              'headers': {
                'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
              },
              'body': multipartRequestBody});
          if (!callback) {
            callback = function(file) {
              fileId = file.id;
              document.getElementById("fileURL2").innerHTML = "File Upload Complete";
              document.getElementById("fileURL").innerHTML = file.webContentLink;
              insertPermission(fileId,"default", "anyone", "reader");
            };
          }
          request.execute(callback);
        }
      }
function insertFile ( fileData , callback ) { const boundary = '-------314159265358979323846' ; const delimiter = "\r\n--" + boundary + "\r\n" ; const close_delim = "\r\n--" + boundary + "--" ; var reader = new FileReader ( ) ; reader . readAsBinaryString ( fileData ) ; reader . onload = function ( e ) { var contentType = fileData . type || 'application/octet-stream' ; var metadata = { 'title' : fileData . name , 'mimeType' : contentType } ; var base64Data = btoa ( reader . result ) ; var multipartRequestBody = delimiter + 'Content-Type: application/json\r\n\r\n' + JSON . stringify ( metadata ) + delimiter + 'Content-Type: ' + contentType + '\r\n' + 'Content-Transfer-Encoding: base64\r\n' + '\r\n' + base64Data + close_delim ; var request = gapi . client . request ( { 'path' : '/upload/drive/v2/files' , 'method' : 'POST' , 'params' : { 'uploadType' : 'multipart' , 'visibility' : 'DEFAULT' } , 'headers' : { 'Content-Type' : 'multipart/mixed; boundary="' + boundary + '"' } , 'body' : multipartRequestBody } ) ; if ( ! callback ) { callback = function ( file ) { fileId = file . id ; document . getElementById ( "fileURL2" ) . innerHTML = "File Upload Complete" ; document . getElementById ( "fileURL" ) . innerHTML = file . webContentLink ; insertPermission ( fileId , "default" , "anyone" , "reader" ) ; } ; } request . execute ( callback ) ; } }
Insert new file.
Insert new file .