question

DamaniaHarsh-9949 avatar image
0 Votes"
DamaniaHarsh-9949 asked MichaelHan-MSFT commented

upload a file larger than 4mb using ms graph powershell

Hi , i am trying to upload an xlsx file larger than 4mb, but it gets corrupted.
Text file works fine

[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

microsoft-graph-sites-lists
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

MichaelHan-MSFT avatar image
0 Votes"
MichaelHan-MSFT answered MichaelHan-MSFT commented

Hi @DamaniaHarsh-9949,

The body should be in bytes to upload the file:

 $body = $fileInBytes


If an Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@DamaniaHarsh-9949,

Is there anything update? Did my solution work for you?

0 Votes 0 ·