Home > Mobile >  RestApi Excel file upload (update) corrupts the file
RestApi Excel file upload (update) corrupts the file

Time:09-14

I have the following PowerShell scrip which reads an .xlsx input file from the file system and uploads it to a page in Confluence:

Param
(
[Parameter(Mandatory=$true)] [string]$userName,
[Parameter(Mandatory=$true)] [string]$userPassword,
[Parameter(Mandatory=$true)] [string]$fileName,
[Parameter(Mandatory=$true)] [string]$filePath
)
$wpURL = "https://xxxxxxxxxxxxxxxxxxxx/rest/api/content/"
$secStringPassword = ConvertTo-SecureString $userPassword -AsPlainText -Force
$Headers = @{'Authorization' = "Basic " [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(($userName ":" [System.Runtime.InteropServices.marshal]::PtrToStringAuto([System.Runtime.InteropServices.marshal]::SecureStringToBSTR($secStringPassword)) )))
            'X-Atlassian-Token' = 'nocheck'
}

$pageId = "xxxxxxxxx"

$uri = $wpURL   $pageId   "/child/attachment?expand=body.storage,version,space,ancestors"
$listOfAttachments = Invoke-WebRequest -Method GET -Headers $Headers -Uri $uri  | ConvertFrom-Json
$listOfAttachments.results
foreach($result in $listOfAttachments.results){
    if($result.title -eq $fileName){
         $attachmentId = $result.id
    }
}

$fileBytes = [System.IO.File]::ReadAllBytes($filePath);
$fileEncode = [System.Text.Encoding]::GetEncoding('UTF-8').GetString($fileBytes);
$delimiter = [System.Guid]::NewGuid().ToString(); 
$LF = "`r`n";
$bodyData = ( 
    "--$delimiter",
    "Content-Disposition: form-data; name=`"file`"; filename=`"$fileName`"",
    "Content-Type: application/octet-stream$LF",
    $fileEncode,
    "--$delimiter--$LF" 
) -join $LF

Invoke-RestMethod -Uri $uri -Method Post -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $bodyLines
$attachmentIdUriPath = "/child/attachment/$attachmentId/data"
$uri = $ConfluenceURL   $pageId   "/child/attachment/"   $attachmentId.ToString()   "/data"
Invoke-RestMethod -Uri $uri -Method POST -ContentType "multipart/form-data; boundary=`"$delimiter`"" -Headers $Headers -Body $bodyData

It works to the extend that it does read and upload the file, the file is not empty and looks like an Excel file, I can download it from the web page, BUT I cannot open it as it is corrupt.

I also see this error while running it in PS:

Invoke-RestMethod : XSRF check failed
At C:\UiPath\Processes\ERP_ReleaseNotesUploadToClientWeb\Data\PSScript.ps1:38 char:1
  Invoke-RestMethod -Uri $uri -Method Post -ContentType "multipart/form ...
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
      FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

so, I have a tip that something is wrong with -ContentType in the last line, but have not figured out what on earth might be wrong.

Maybe some fresh eyes could help?

CodePudding user response:

OK, strangely enough the corruption occurs because of the "UTF-8" encoding. It worked fine when I put "iso-8859-1" instead ...

  • Related