2

Seems like this should be easier. But admittedly I don't understand blobs.

function doGet(e) { 
  var app = UiApp.createApplication(); 
  var panel = app.createVerticalPanel().setId('panel');
  var fileUpload = app.createFileUpload().setName('theFile').setId('theFile');
  var handler = app.createServerChangeHandler('uploadfile');
  handler.addCallbackElement(panel);
  fileUpload.addChangeHandler(handler);
  panel.add(fileUpload);
  app.add(panel);
  return app;
} 

function uploadfile(e) 
{ 
// data returned which can be used to create a blob
// assuming mime-type to be a text file in this example
  var fileBlob = Utilities.newBlob(e.parameter.thefile, "text/plain","file.txt" );

    // Create a new file
    var doc = DocumentApp.create('Uploaded Text File');

    doc.appendParagraph(fileBlob.getDataAsString());

  // Save and close the document
  doc.saveAndClose();

  //var doc = DocsList.createFile(fileBlob.getDataAsString());
  var app = UiApp.getActiveApplication();
  app.getElementById('panel').add(app.createLabel('File Uploaded successfully'));
  return app;
}

I keep getting undefined returned when I attempt to upload a file using this Google Apps Script Code. All I want to do is upload a text file to my Google Drive.

What should I do to fix this code? or is there a better way to do this?

jth41
  • 3,808
  • 9
  • 59
  • 109
  • Works just fine for me, testing using the `/dev` URL. If you're testing with the published URL, have you made sure to save and publish the latest version? – Mogsdad Sep 10 '13 at 02:48
  • It uploads a file but all the file contains is "undefined" – jth41 Sep 10 '13 at 02:53
  • Ah - when you said you got undefined "returned" I thought of a function return code for some reason. – Mogsdad Sep 10 '13 at 02:57
  • my bad. I should have explained that better – jth41 Sep 10 '13 at 02:59
  • possible duplicate of [File upload at google apps script](http://stackoverflow.com/questions/12264423/file-upload-at-google-apps-script) – Srik Sep 10 '13 at 03:27
  • Weird... I _did_ repeat the experience of a file containing "undefined"... but when I run the same code now, I get a file full of blob data - but not contents. – Mogsdad Sep 10 '13 at 04:02
  • I got it figured out with the dudes answer now just the home stretch: http://stackoverflow.com/questions/18710624/create-new-doc-in-google-drive-after-processing-uploaded-text-file – jth41 Sep 10 '13 at 04:16

2 Answers2

1

Google Apps Script now has "Experimental" Resources to Advanced Google Services. I counted 15 Advanced Services, including Drive API.

enter image description here

Click the Resources menu:

enter image description here

You must also go to the Google Developers console, and enable Drive Service there also.

The Google Documentation is at: Advanced Drive Services

Developers Console

Here is some sample code from Google:

function uploadFile() {
  var image = UrlFetchApp.fetch('http://goo.gl/nd7zjB').getBlob();
  var file = {
    title: 'google_logo.png',
    mimeType: 'image/png'
  };
  file = Drive.Files.insert(file, image);
  Logger.log('ID: %s, File size (bytes): %s', file.id, file.fileSize);
}

EDIT

I thought there was no way to upload a file from the users computer to Google drive using the HTML Service, but luckily I'm wrong again! :)

File Upload with HTML Service

Community
  • 1
  • 1
Alan Wells
  • 30,746
  • 15
  • 104
  • 152
0

To do a file upload, you cannot use a server handler. You must use a FormPanel and a Submit button. See the FileUpload widget's documentation.

Srik
  • 7,907
  • 2
  • 20
  • 29
  • Saw that in [James' older answer](http://stackoverflow.com/a/8097797/1677912), but @waqar-ahmad posted a [tutorial](https://sites.google.com/site/appsscripttutorial/user-interface/upload-doc) with a change handler doing the upload. Generally, I trust him... but this isn't working today. – Mogsdad Sep 10 '13 at 04:01
  • The documentation link in this answer, and the tutorial link in the comment both use Google's UI Service FileUpload Class. The problem is, if you already have your application designed with HTML Service, you can't use the UI Service together with the HTML Service. But there is a better way: [File Upload with HTML Service](http://stackoverflow.com/questions/20769149/form-and-file-upload-with-htmlservice-and-app-script-not-working?rq=1) – Alan Wells Apr 21 '14 at 02:18