PrivMX DOCS
JavaScript

Files

Uploading and managing files.

The sample code on this page is based on the same assumptions

Managing Files

Creating files in Stores with additional metadata:

// for example, let's put some extra file info in the file's private meta
const sampleFileInfo = {
    name: file.name,
    mimetype: file.type
};
 
const sampleFileData = serializeObject("sample file data");
 
const fileHandle = await storesApi.createFile(storeId,
    serializeObject("file's public meta-data"),
    serializeObject(sampleFileInfo),
    sampleFileData.length
);
 
await storesApi.writeToFile(fileHandle, sampleFileData);
const fileId = await storesApi.closeFile(fileHandle);

A detailed code sample of how to upload (stream) a large file can be found here.

Modifying Files

// Example: let's update given file's private meta to reflect the file's name change.
const currentFile =  storeApi.getFile(
    fileId
);
 
const currentFilePrivateMeta = deserializeObject(currentFile.privateMeta);
 
const newFilePrivateMeta = {
    name: "newFileName"
};
await storeApi.updateFile(
    fileId,
    currentFile.publicMeta,
    serializeObject(newFilePrivateMeta),
    currentFile.fileSize
);

We use cookies on our website. We use them to ensure the proper functioning of the site and, if you agree, for purposes we set, such as analytics or marketing.

On this page