createSrc()

createSrc function is used for reading the uploaded file.

Here is an example:

Frontend:

import { createUploader, createSrc } from '@magicjs.dev/frontend';
import getFileServer from './get-file.server'; // The backend server function for reading files

const fileSrc = createSrc(getFileServer);

return (
  <div>
    <img src={fileSrc.getLink('fileName.jpg')} alt="File Preview" />
  </div>
);

Backend get-file.server

import { createBackendFunction, utils } from '@magicjs.dev/backend';

export default createBackendFunction(async function (fileName: string) {
  return utils.readFileFromUserUploads('/path', fileName);
});

Click here to refer GitHub.

Last updated