IPWorks Cloud 2020 macOS Edition

Questions / Feedback?

UploadFile Method

Uploads, and optionally converts, a new file.

Syntax

public func uploadFile(fileName: String, mimeType: String) throws -> String

Remarks

This method uploads a new file and returns the Id of the newly-created file resource.

If LocalFile is set the file will be uploaded from the specified path. If LocalFile is not set the data in ResourceData will be used.

Usage

The name specified by the FileName parameter should include a file extension.

If MimeType is "", the file will be uploaded as-is and its MIME type will be automatically determined. The file can be converted to one of the Google formats by explicitly setting MimeType to one of the MIME types in the table below.

Before calling this method, always set ResourceCount to 0 to clear the stored resource data. Then, if you wish to set file metadata and/or assign parent Ids when the file is uploaded, set ResourceCount to 1 and set the desired properties. (Note that the name and MIME type passed to this method will take precedence.)

To encrypt the file before uploading it, set EncryptionAlgorithm and EncryptionPassword.

Finally, call this method to upload the file, along with any metadata that may be set.

Original File Format Google FormatMIME Type
MS Word, OpenDocument Text, HTML, RTF, Plain Text Google Docs application/vnd.google-apps.document
MS Excel, OpenDocument Spreadsheet, CSV, TSV, Plain Text Google Sheets application/vnd.google-apps.spreadsheet
MS Powerpoint, OpenDocument Presentation Google Slides application/vnd.google-apps.presentation
PDF, JPEG, PNG, GIF, BMP (images are embedded in a Doc) Google Docs application/vnd.google-apps.document
Plain Text, JSON Google Apps Script application/vnd.google-apps.script

Upload Notes

Google Drive offers two ways to upload a file. For smaller files a simple upload option is provided to upload data in one request. This is the default option. For larger files, uploads can be fragmented into multiple pieces, allowing resuming of uploads that may be interrupted.

Simple

By default the class uses the simple upload mechanism.

googledrive.LocalFile = "../MyFile.zip";
googledrive.UploadFile("MyFile.zip", "");

Resumable

To enable resumable uploads set UseResumableUpload to . This is recommended for large files. The class will automatically fragment the specified file into smaller pieces and upload each individually. FragmentSize may be set to specify the size of the fragment if desired. The default fragment size is 10 MB.

When UseResumableUpload is set to and UploadFile is called, a resumable upload session is started by the class. ResumeURL is populated with a URL identifying the session (this value may be needed for additional operations if the upload does not complete normally).

During a resumable upload, the FragmentComplete event fires after each fragment is uploaded to indicate overall progress. The class also updates StartByte as necessary to indicate the current offset in the file.

If the upload is interrupted for any reason, resuming it is easy. First, verify that ResumeURL and StartByte are populated (if the same instance of the class is used, they should already be populated, and no special action should be needed). Then call UploadFile again to resume the upload at the specified StartByte offset.

Note that if the upload is not resumed after some time the upload session will expire. GetUploadStatus may be used to check the status of a resumable upload.

googledrive.LocalFile = "../MyFile.zip";
googledrive.UploadFile("MyFile.zip", "");

//The transfer is interrupted and UploadFile() above fails. Later, resume the download.
//Using the same instance StartByte and ResumeURL are already populated from the previous
//upload attempt.
googledrive.UploadFile("MyFile.zip", "");

Copyright (c) 2022 /n software inc. - All rights reserved.
IPWorks Cloud 2020 macOS Edition - Version 20.0 [Build 8265]