Sometimes you may wish to store additional data with files that users upload such as form data or custom JSON data. The FileSpin.update API allows you to easily do this. The API takes three parameters:-
Upload ID
, a unique ID that is returned when you open the Upload dialog withFileSpin.initPicker()
API- A dictionary object with
Asset ID
as key and JSON data as value - note that file ID is returned in thecomplete
upload event - A callback function as a third parameter to handle API call success or error
Example that shows how to save some data with a file including searchable keywords
//Initialize Picker and get an uploadID
var uploadID = FileSpin.initPicker({
uploadKey: "UPLOAD_KEY",
});
//prepare JSON data you want to save with the file
//use "filespin_search_txt" to add searcheable keywords
var payload = {
"Asset ID 1": {
foo: "bar",
filespin_search_txt: "sample search keywords",
},
"Asset ID 2": { foo: "bar" },
};
//Save the data
FileSpin.update(uploadID, payload, function (err) {
if (err) {
// data update failed
// err will be "UPDATE_UNAUTHORIZED" or "UPDATE_FAILED"
} else {
// data storage succeeded
}
});
If filespin_search_txt
is present in the JSON data, it is treated as a special key whose value will be indexed for search using Search API.
You can update arbitrary data against all or any file id returned in the complete
event. Simply pass the data against each file id you would like to be updated.
JSON Schema for Custom Data
FileSpin optionally supports JSON Schema for Asset Form Data. Assigning schema to asset data enables versatile field-based search and additional data validations.
Once Asset schemas have been setup for an account, you can pass the schema id __data_schema_id
as shown below along with form data to automatically assign a schema to the asset data.
//pass "__data_schema_id" to assign a JSON Schema for the asset data
var payload = {
"Asset ID 1": {
fieldA: "value1",
fieldB: "value2",
"__data_schema_id": 1,
},
"Asset ID 2": {
fieldA: "value11",
fieldB: "value22",
__data_schema_id: 1,
},
};
Note that __data_schema_id
is a reserved key to indicate the schema to be used and will not be stored as part of the form data.
For enabling schema support, Please contact support.