Upload a xlsx file larger than 4mb in ms graph powershell fails

Viewed 102

It works for text files, but fails for xlsx. it gives no error, and file is corrupted. Not sure if the content type is correct

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12

$filename = "Test.xlsx"

$api = "https://graph.microsoft.com/v1.0/sites/10b44bd9-97fa-4666-b735-6ac0868a04c1/drive/root:/General/"+$filename+":/createUploadSession"
$responseCreateUploadSession = Invoke-RestMethod $api -Method 'POST' -Headers $Headers

## "https://graph.microsoft.com/v1.0/sites/20b44bd9-97fa-4666-b735-6ac0868a04c1/drive/items/01CLOFTIEVJ25ZIZLHKVA3Q3HXB67OWT2G/createUploadSession" 

$filePath = "C:\Users\Downloads\"+$filename
# Convert the file into ascii code and get the file size in bytes
$fileInBytes = [System.IO.File]::ReadAllBytes($filePath)
$fileInAscii = [System.Text.Encoding]::ASCII.GetString($fileInBytes)
$fileLength = $fileInAscii.Length
$accesstoken=$Headers.Values
$headersNew = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headersNew.Add("Content-Length", $fileLength)
$headersNew.Add("Content-Range", "bytes 0-$($fileLength-1)/$($fileLength)")
$headersNew.Add("Authorization", $accesstoken)

$headersNew.Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")

$body = $fileInAscii

$responseUpload = Invoke-RestMethod $responseCreateUploadSession.uploadUrl -Method 'PUT' -Headers $headersNew -Body $body
0 Answers
Related