User Manual

08.14.2024

MANUAL

FAQ: I want to batch download attachments of multiple records in a batch process

## Answer Please use the "[Multiple Records Retrieval API](/en/manual/api-record-get-multi)" and "「Attachment Retrieval API」". --- ## Overview If you want to download attachments attached to multiple records in a table in batch processing, use the "[Multiple Records Retrieval API](/en/manual/api-record-get-multi)" and "「Attachment Retrieval API」". This page introduces batch processing in PowerShell. ## Limitations 1. Only one table can be specified. 1. Only one attachment item can be retrieved. ## Operation Procedure 1. Create a table and attach a file to the record. 1. Open Windows PowerShell and write the following script. Modify the "Variable Definition" according to your environment. 1. Execute the PowerShell script you created. ### Execution result A folder with the record ID is created in C:Works, and the file attached to the Attachment A item is downloaded under it. ![image](https://pleasanter.org/binaries/8db92da1aef14c87814a8470c0a15230) ![image](https://pleasanter.org/binaries/42fd88ed9adc4af0a064f40fcec5969f) ## Sample Code ##### PowerShell ``` <# ***************************** Variable definition location User must change ***************************** #> # Download path (specify as "C:\work") $toPath = 'XXXX' # On Pleasanter (specify as protocol + domain "https://pleasanter.net/fs") $baseUri = 'XXXX' # API key $apiKey = 'XXXX' # Site ID of table $siteId = 'XXXX' # Initialize offset $offSet = 0 # Set ApiVersion $apiVersion = 0 # Set method $method = 'XXXX' # Set ContentType $contentType = 'XXXX' # Select attachment item to get $attachment = 'XXXX' <# ***************************** Logic definition part No changes required by users ***************************** #> try { # File download process do { # JSON contents $jsonData = @{ 'ApiVersion' = $apiVersion 'ApiKey' = $apiKey 'Offset' = $offSet } # Convert to text format $json = $jsonData|ConvertTo-Json # Set URI $itemUri = $baseUri + '/api/items/' + $siteId + '/get' # Access a web page and assign the response $responseData = Invoke-WebRequest -Uri $itemUri -ContentType $contentType -Method $method -Body $json # Assign the information used in the process up to one level $response = ($responseData.Content|ConvertFrom-Json).Response # Assign the number of records that can be held per page $pageSize = $response.PageSize # Assign the total number of records recorded in the table $totalCount = $response.TotalCount # Assign record information $records = $response.Data # Iterating through records foreach ($record in $records) { # Assign the record's ResultId $id = $record.ResultId # Assign download destination folder path $folderPath = $toPath + '\' + $id # If the folder does not exist, continue processing if (-not(Test-Path $folderPath)) { # Create a download folder New-Item -Path $folderPath -Type Directory # If the folder exists } else { write-host 'Folder exists' } # Assign the contents of the response's AttachmentsHash $recordLinks = $record.AttachmentsHash # Iterating over data foreach ($recordLink in $recordLinks){ # Assign the contents of AttachmentsA $attachments = $recordLink.$attachment # Iterating over files foreach ($attachment in $attachments) { # Assign file name $name = $attachment.Name # Assign Guid $guId = $attachment.Guid # If the file exists in the download source, continue processing. if (-not($name -eq $null)) { # Assign the download source path based on the GUID $uri = $baseUri + '/api/binaries/' + $guId + '/get' # Access a web page and assign the response $responseData = Invoke-WebRequest -Uri $uri -ContentType $contentType -Method $method -Body $json # Assign Base64 to each file $base64 = ($responseData.Content|ConvertFrom-Json).Response.Base64 # Decode $base64 $byte = [System.Convert]::FromBase64String($base64) # Assign output file name $toFileName = $toPath + '\' + $id + '\' + $name # Continue if file does not exist if (-not(Test-Path $toFileName)) { # Create a file in the download destination New-Item -Path $folderPath -Name $name -Type File # Write $src to the destination file [System.IO.File]::WriteAllBytes($folderPath + '\' + $name,$byte) # If the file exists } else { Write-Host 'File exists' } } } } } $offSet = $offSet + $pageSize } while ($totalCount -ge $offSet) write-host 'Download completed' } catch { $_.Exception.Response } ``` ## Related Information <div id="ManualList"><ul><li><a href="/en/manual/api-record-get-multi">Developer Function: API: Table Operation: Retrieve Multiple Records</a><span>08.14.2024 up</span></li></ul></article></div><input id="SearchTextHidden" type="hidden" value="" />
TOP
このページをシェアする
記載された商品名、各製品名は各社の登録商標または商標です。 © Implem Inc.