Skip to content

I want to download attachments in a batch process

Answer

Use the "Attachment Retrieval API".


Overview

If you want to download attachments in a batch process, use the "Attachment Retrieval API".
Use PowerShell to download attachments for any record.

Operation Procedure

  1. Create a table and attach a file to the record.
  2. Open Windows PowerShell and write the following script content.
  3. Change the values ​​of [servername], $requestUrl, and $apiKey to suit your environment.
$requestUrl = "http://[servername]/api/binaries/[Guid]/get"
$apiKey = "[api key]"

To get the Guid of a record, follow the link below.
API Function: Retrieve Single Record

Execution Result

The attachments will be downloaded to C:Works.
Attachment downloaded to C:\Works

Code Samples

PowerShell
Add-Type -AssemblyName "System.Web"
$error.Clear()
$requestUrl = "http://[servername]/api/binaries/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/get"
$apiKey = "XXXXXXXX..."
$path = "C:\Works"
trap [Net.WebException] { continue; }
try{
    $json = @{
        ApiKey = $apiKey
    }
    $requestBody = $json | ConvertTo-Json -Depth 3
    $res = Invoke-RestMethod -Uri $requestUrl -ContentType "application/json" -Method POST -Body ${requestBody}
    Write-Output $res
    $byte = [System.Convert]::FromBase64String($res.Response.Base64)
    [System.IO.File]::WriteAllBytes($path + "\" + $res.Response.FileName, $byte)
}
catch {
    Write-Output $_.Exception
}
if ($error.Count -gt 0)
{
    Write-Output $error[0].ErrorDetails.Message | ConvertFrom-Json
}