Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

C# UWP/WinRT Examples

Web API Categories

ASN.1
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Azure Cloud Storage
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(C# UWP/WinRT) OneDrive -- Upload Large Files with an Upload Session

See more OneDrive Examples

Demonstrates how to upload large files with an upload session. See OneDrive Upload Session for more general information.

Note: This example requires Chilkat v9.5.0.68 or greater.

Chilkat Universal Windows Platform (UWP) / WinRT Downloads

Chilkat for the Universal Windows Platform (UWP)

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

Chilkat.Http http = new Chilkat.Http();
bool success;

// Use your previously obtained access token here:
// See the following examples for getting an access token:
//    Get Microsoft Graph OAuth2 Access Token (Azure AD v2.0 Endpoint).
//    Get Microsoft Graph OAuth2 Access Token (Azure AD Endpoint).
//    Refresh Access Token (Azure AD v2.0 Endpoint).
//    Refresh Access Token (Azure AD Endpoint).

// (Make sure your token was obtained with the Files.ReadWrite scope.)
http.AuthToken = "MICROSOFT_GRAPH_ACCESS_TOKEN";

// ----------------------------------------------------------------------------
// Step 1: Create an upload session

// To begin a large file upload, your app must first request a new upload session. This creates a 
// temporary storage location where the bytes of the file will be saved until the complete file is uploaded. 
// Once the last byte of the file has been uploaded the upload session is completed and the final file is shown 
// in the destination folder.

// Send the following POST to create an upload session:
// If not using "me", then the path should be /v1.0/users/{id | userPrincipalName}/...
// POST /v1.0/me/drive/root:/{path_to_item}:/createUploadSession

http.SetUrlVar("path_to_item","/somefolder/big.zip");
string url = "https://graph.microsoft.com/v1.0/me/drive/root:/{$path_to_item}:/createUploadSession";
Chilkat.HttpResponse resp = await http.PostJson2Async(url,"application/json","{}");
if (http.LastMethodSuccess != true) {
    Debug.WriteLine(http.LastErrorText);
    return;
}

// If successful, a 200 status code is returned, with the session details (in JSON format).
Chilkat.JsonObject jsonSession = new Chilkat.JsonObject();
jsonSession.EmitCompact = false;
jsonSession.Load(resp.BodyStr);

if (resp.StatusCode != 200) {

    Debug.WriteLine(jsonSession.Emit());
    Debug.WriteLine("Response status = " + Convert.ToString(resp.StatusCode));

    return;
}

Debug.WriteLine(jsonSession.Emit());

// A sample response:

// 	{
// 	  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#microsoft.graph.uploadSession",
// 	  "uploadUrl": "https://api.onedrive.com/rup/3a33fceb9b74cc15/eyJSZXNvdXJjZUlEI ... 65yDYUiS3JTDnnhqCHxw",
// 	  "expirationDateTime": "2017-06-11T12:40:23.239Z",
// 	  "nextExpectedRanges": [
// 	    "0-"
// 	  ]
// 	}
// 

// ----------------------------------------------------------------------------
// Step 2: Upload Data in Segments (a.k.a. Fragments or Chunks)

// Microsoft states this requirement: Use a fragment size that is a multiple of 320 KiB (320 * 1024 bytes). 
// Failing to use a fragment size that is a multiple of 320 KiB can result in large file transfers failing after the 
// last fragment is uploaded.  (Note: This is a detail imposed by Microsoft's OneDrive server-side implementation.)

int fragSize = 320 * 1024;
string localFilePath = "qa_data/zips/big.zip";

// Upload the file big.zip in 320KiB segments.
// Note: The FileSize method returns a signed 32-bit integer.  If the file is potentially larger than 2GB, call FileSizeStr instead to return
// the size of the file as a string, then convert to an integer value.
Chilkat.FileAccess fac = new Chilkat.FileAccess();
int fileSize = fac.FileSize(localFilePath);

// Open the file to get the number of fragments.
success = fac.OpenForRead(localFilePath);
if (success == false) {
    Debug.WriteLine(fac.LastErrorText);
    return;
}

int numFragments = fac.GetNumBlocks(fragSize);
fac.FileClose();

int i = 0;

Debug.WriteLine("fileSize = " + Convert.ToString(fileSize));
Debug.WriteLine("numFragments = " + Convert.ToString(numFragments));

Chilkat.Url uploadUrl = new Chilkat.Url();
uploadUrl.ParseUrl(jsonSession.StringOf("uploadUrl"));

Chilkat.JsonObject json = new Chilkat.JsonObject();
json.EmitCompact = false;

Chilkat.HttpRequest req = new Chilkat.HttpRequest();
req.HttpVerb = "PUT";
req.Path = uploadUrl.PathWithQueryParams;
req.ContentType = "application/octet-stream";

Chilkat.StringBuilder sbOffset = new Chilkat.StringBuilder();
Chilkat.StringBuilder sbNumBytes = new Chilkat.StringBuilder();
Chilkat.StringBuilder sbRange = new Chilkat.StringBuilder();

// IMPORTANT: The uploadUrl is a temporary URL to be used to upload the fragment.
// It requires no authentication (because the URL itself could only have been obtained from an authenticated
// request to start the upload session).  Therefore, do not allow the upload URL to be publicly seen,
// otherwise anybody could upload to your OneDrive.
http.AuthToken = "";

int bytesRemaining = fileSize;
while (i < numFragments) {

    // The success response code for intermediate chunks is 202,
    // whereas the final chunk will have a 201 success response where
    // the response body is the JSON DriveItem.
    int chunkSize = fragSize;
    int expectedStatusCode = 202;
    if (bytesRemaining < chunkSize) {
        chunkSize = bytesRemaining;
        expectedStatusCode = 201;
    }

    Debug.WriteLine("  this chunkSize = " + Convert.ToString(chunkSize));

    // Indicate the fragment in the local file to be streamed in the upload.
    sbOffset.Clear();
    sbOffset.AppendInt(i * fragSize);
    sbNumBytes.Clear();
    sbNumBytes.AppendInt(chunkSize);
    req.StreamChunkFromFile(localFilePath,sbOffset.GetAsString(),sbNumBytes.GetAsString());

    // The Content-Range header field must be set for this fragment.  For example:
    // Content-Range: bytes 0-25/128
    sbRange.SetString("bytes start-end/fileSize");
    int numReplaced = sbRange.ReplaceI("start",i * fragSize);
    numReplaced = sbRange.ReplaceI("end",i * fragSize + chunkSize - 1);
    numReplaced = sbRange.ReplaceI("fileSize",fileSize);
    req.AddHeader("Content-Range",sbRange.GetAsString());
    Debug.WriteLine("  this content-range: " + sbRange.GetAsString());

    resp = await http.SynchronousRequestAsync(uploadUrl.Host,443,true,req);
    if (http.LastMethodSuccess != true) {
        Debug.WriteLine(http.LastErrorText);
        return;
    }

    json.Load(resp.BodyStr);
    // A 202 response status code indicates success.
    if (resp.StatusCode != expectedStatusCode) {

        Debug.WriteLine(json.Emit());
        Debug.WriteLine("Response status = " + Convert.ToString(resp.StatusCode));

        return;
    }

    Debug.WriteLine(json.Emit());
    Debug.WriteLine("---- Chunk " + Convert.ToString(i) + " uploaded ----");

    bytesRemaining = bytesRemaining - chunkSize;
    i = i + 1;
}

Debug.WriteLine("data uploaded.");

// ----------------------------------------------------------------------------
// Sample output for the above session:

// {
//   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#microsoft.graph.uploadSession",
//   "uploadUrl": "https://api.onedrive.com/rup/3a33fceb9b74cc15/eyJSZXNvd ... QoKK2iuh1A",
//   "expirationDateTime": "2017-06-11T14:04:45.438Z",
//   "nextExpectedRanges": [
//     "0-"
//   ]
// }
// 
// fileSize = 1366807
// numFragments = 5
//   this chunkSize = 327680
//   this content-range: bytes 0-327679/1366807
// {
//   "expirationDateTime": "2017-06-11T14:04:45.438Z",
//   "nextExpectedRanges": [
//     "327680-1366806"
//   ]
// }
// 
// ---- Chunk 0 uploaded ----
//   this chunkSize = 327680
//   this content-range: bytes 327680-655359/1366807
// {
//   "expirationDateTime": "2017-06-11T14:04:45.438Z",
//   "nextExpectedRanges": [
//     "655360-1366806"
//   ]
// }
// 
// ---- Chunk 1 uploaded ----
//   this chunkSize = 327680
//   this content-range: bytes 655360-983039/1366807
// {
//   "expirationDateTime": "2017-06-11T14:04:45.438Z",
//   "nextExpectedRanges": [
//     "983040-1366806"
//   ]
// }
// 
// ---- Chunk 2 uploaded ----
//   this chunkSize = 327680
//   this content-range: bytes 983040-1310719/1366807
// {
//   "expirationDateTime": "2017-06-11T14:04:45.438Z",
//   "nextExpectedRanges": [
//     "1310720-1366806"
//   ]
// }
// 
// ---- Chunk 3 uploaded ----
//   this chunkSize = 56087
//   this content-range: bytes 1310720-1366806/1366807
// {
//   "createdBy": {
//     "application": {
//       "displayName": "Chilkat",
//       "id": "441c9990"
//     },
//     "user": {
//       "id": "3a33fceb9b74cc15"
//     }
//   },
//   "createdDateTime": "2017-06-04T14:04:47.247Z",
//   "cTag": "aYzozQTMzRkNFQjlCNzRDQzE1ITQ4NjguMjU3",
//   "eTag": "aM0EzM0ZDRUI5Qjc0Q0MxNSE0ODY4LjA",
//   "id": "3A33FCEB9B74CC15!4868",
//   "lastModifiedBy": {
//     "application": {
//       "displayName": "Chilkat",
//       "id": "441c9990"
//     },
//     "user": {
//       "id": "3a33fceb9b74cc15"
//     }
//   },
//   "lastModifiedDateTime": "2017-06-04T14:04:47.247Z",
//   "name": "big.zip",
//   "parentReference": {
//     "driveId": "3a33fceb9b74cc15",
//     "id": "3A33FCEB9B74CC15!4862",
//     "name": "someFolder",
//     "path": "/drive/root:/someFolder"
//   },
//   "size": 1366807,
//   "webUrl": "https://1drv.ms/u/s!ABXMdJvr_DM6pgQ",
//   "file": {
//     "hashes": {
//       "sha1Hash": "252059AA13004220DB912B97D4D3FF9599CCD8D9"
//     },
//     "mimeType": "application/zip"
//   },
//   "fileSystemInfo": {
//     "createdDateTime": "2017-06-04T14:04:47.246Z",
//     "lastModifiedDateTime": "2017-06-04T14:04:47.246Z"
//   },
//   "tags": [
//   ],
//   "lenses": [
//   ]
// }
// 
// Response status = 201

 

© 2000-2022 Chilkat Software, Inc. All Rights Reserved.