For files bigger then 4MB you need to create an uploadSession that you POST to this URL:
https://graph.microsoft.com/v1.0/me/drive/root:/{item-path}:/createUploadSession
Pass an array of items,
{
"item": {
"@odata.type": "microsoft.graph.driveItemUploadableProperties",
"@microsoft.graph.conflictBehavior": "rename",
"name": "largefile.dat"
}
}
I use "@microsoft.graph.conflictBehavior": "overwrite"
, instead of rename
.
The response will provide an upload url to upload the file in batches
{
"uploadUrl": "https://sn3302.up.1drv.com/up/fe6987415ace7X4e1eF866337",
"expirationDateTime": "2015-01-29T09:21:55.523Z"
}
I don't have a c# example but here's a PHP one:
$url = $result['uploadUrl'];
$fragSize = 1024*1024*4;
$file = file_get_contents($source);
$fileSize = strlen($file);
$numFragments = ceil($fileSize / $fragSize);
$bytesRemaining = $fileSize;
$i = 0;
$response = null;
while ($i < $numFragments) {
$chunkSize = $numBytes = $fragSize;
$start = $i * $fragSize;
$end = $i * $fragSize + $chunkSize - 1;
$offset = $i * $fragSize;
if ($bytesRemaining < $chunkSize) {
$chunkSize = $numBytes = $bytesRemaining;
$end = $fileSize - 1;
}
if ($stream = fopen($source, 'r')) {
// get contents using offset
$data = stream_get_contents($stream, $chunkSize, $offset);
fclose($stream);
}
$contentRange = " bytes " . $start . "-" . $end . "/" . $fileSize;
$headers = array(
"Content-Length: $numBytes",
"Content-Range: $contentRange"
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$server_output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$bytesRemaining = $bytesRemaining - $chunkSize;
$i++;
}