File operations

This page introduces APIs for uploading and downloading files.

Upload File

Method

POST

URI
[your kumaneko domain]/api/file.php?name=file&dir=attachment
Authentication

Add "X-Authorization" to the request header and specify the Base64 encoded account and password as values. Base64 encoding should be in the form "account: password".

Content-Type

multipart/form-data

Parameters

The request is sent in a multipart/form-data format.

Within the Content-Disposition header, specify "file" for the name parameter and the name of the file for the filename parameter.

Response

The fileKey of the uploaded file will be returned.

{
	files: [
		{
			filekey: "16575023862769001.png",
			filetype: "image/png",
			name: "test.png"
		}
	]
}
Sample

JavaScript

var blob = new Blob(['Sample File'], {type:'text/plain'});
var fileData = new FormData();
fileData.append('file[]', blob, 'test.txt');
fetch('[your kumaneko domain]/api/file.php?name=file&dir=attachment', {
	method: 'POST',
	headers: {
		'X-Authorization': window.btoa(pd.kumaneko.users.login().account.value+':'+pd.kumaneko.users.login().pwd.value)
	},
	body: fileData
})
.then(response => {
	response.json().then((json) => {
		switch (response.status)
		{
			case 200:
				console.log(json);
				break;
			default:
				console.error('Error:', json);
				break;
		}
	});
})
.catch(error => {
	console.error('Error:', error);
});

curl

curl -X POST '[your kumaneko domain]/api/file.php?name=file&dir=attachment' \
	-H 'X-Authorization: L08xCvTh7A1EVm3rZimF98R8VLP3k4lMlzELqyCx' \
	-F 'file[]=@FILEPATH'

Download File

Method

GET

URI
[your kumaneko domain]/api/file.php
Authentication

Add "X-Authorization" to the request header and specify the Base64 encoded account and password as values. Base64 encoding should be in the form "account: password".

Content-Type

application/json

Parameters
Parameter Value Description
filekey String

The value set in the file field of the record object.

A record object is an object that contains record information such as field codes and field values.

References:

Field types

dir String It is fixed to "attachment".
Response

The base64-encoded data.

{
	file: "44CQUHJvcG9uZeOAkeOBlOWlkee0hOiAhemZkOWumlRJU+OD..."
}

If the file size of the file to be downloaded exceeds the "memory_limit" set in php.ini, the file will be divided and downloaded.

In that case, a parameter called "seek" will be added, so check the download method while looking at the sample code.

{
	file: "44CQUHJvcG9uZeOAkeOBlOWlkee0hOiAhemZkOWumlRJU+OD...",
	seek: 25165824
}
Sample

JavaScript

var url = '[your kumaneko domain]/api/file.php?filekey=16575023862769001.png&dir=attachment';
var files = [];
var getFile = (url) => {
	fetch(url, {
		method: 'GET',
		headers: {
			'X-Authorization': window.btoa(pd.kumaneko.users.login().account.value+':'+pd.kumaneko.users.login().pwd.value)
		}
	})
	.then(response => {
		response.json().then((json) => {
			switch (response.status)
			{
				case 200:
					if (json.seek)
					{
						files.push(json.file);
						getFile(url+'&seek='+json.seek.toString());
					}
					else
					{
						if (files.length!=0)
						{
							files.push(json.file);
							json.file=files.join('');
						}
						console.log(json);
					}
					break;
				default:
					console.error('Error:', json);
					break;
			}
		});
	})
	.catch(error => {
		console.error('Error:', error);
	});
};
getFile(url);

curl

curl -X GET '[your kumaneko domain]/api/file.php' \
	-H 'X-Authorization: L08xCvTh7A1EVm3rZimF98R8VLP3k4lMlzELqyCx' \
	-H 'Content-Type: application/json' \
	-d '{
		"fileKey": "16575023862769001.png",
		"dir": "attachment"
	}'