Working with files
Unimicro uses a separate endpoint for uploading and downloading files. This endpoint handles upload and download as well as features like OCR-scanning and image-conversion.
The endpoint for testing is "https://test-files.unimicro.no" but you can always get the list of related endpoints for the given platform by calling the regular API on "/api/endpoints"
Uploading files
File upload uses the "MultipartFormData" encoding algorithm. This means that you POST data as a HTML form (FormData Objects (mozilla.org) ). This simplifies the upload for browser-based applications, but can be a bit more complex from a service or a console-application.
The required form-fields are:
- Token
- The current authorization-token
- Key
- The companykey (active company)
- File
- The actual file being uploaded
Example from C# (.net core):
var _token = "***";
var _companyKey = "1f2ace3-23435...";
var _fileName = @"c:\files\mytestfile.pdf";
var _fileApiRoute = "https://test-files.unimicro.com";
//Create form-data
var form = new MultipartFormDataContent();
form.Add(new StringContent(_token), "Token");
form.Add(new StringContent(_companyKey), "Key");
var fileContent = new StreamContent(new FileStream(_fileName, FileMode.Open));
fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data")
{
Name = "File",
FileName = Path.GetFileName(_fileName)
};
form.Add(fileContent);
// Upload
using var client = new HttpClient();
client.BaseAddress = new Uri(_fileApiRoute);
var result = client.PostAsync("api/file", form).Result;
Response
The response from uploading a file is a JSON representation of the uploaded file:
{
"Id": 456789,
"Name": "mytestfile.pdf",
"Description": null,
"StorageReference": "6d029bfc-f661-45e5-b927-f4a80405b8ca",
"PermaLink": "/3b451469-bc84-42ef-a54a-88b3ccd3863d/6d029bfc-f661-45e5-b927-f4a80405b8ca",
"ContentLength": 2418,
"Extension": ".pdf",
"ContentType": "application/pdf",
"CreatedAt": "2021-12-17T10:58:59.7648684Z",
"ClientId": 2346,
"Pages": 1,
"ExternalId": "123",
"ExternalValidation": 0,
"IsMultipageFile": true
}
The StorageReference allows you to perform operations on the file directly with the files-API. The ExternalId is the most important reference if you want to work with the file afterwards in relation to the regular API.
Downloading files
To download the file use the "api/download" endpoint on the file-service. Build a URI with the following arguments:
- https://{files-endpoint}/api/download?id={StorageReference}
- &key={companykey}
- &token={token}
Fileconversion
To get a file as an image you can use the "api/image" endpoint on the file-service. This can be useful if you dont have a PDF-viewer in your application.
Build a URI with the following arguments:
- https://{files-endpoint}/api/image?key={StorageReference}
- &width=1200
- &page=1
Note! If your not using a single-tenant token, make sure you put the "Authorization: Bearer token" in header to get access to the file.