How to rename a file in uppy? javascript

1.8k views Asked by At

I am trying to rename files in uppy.js. According to the documentation, there are 2 functions that are capable of providing this functionality. I defined both of these functions, however, they are having no effect on the file name and no errors are being thrown. How can I use these functions to rename a file?

onBeforeFileAdded: (currentFile, files) => {
  const modifiedFile = {
    ...currentFile,
    name: currentFile.name + '__' + Date.now()
  }
  return modifiedFile
}

onBeforeUpload: (files) => {
  if (Object.keys(files).length < 2) {
    // log to console
    uppy.log(`Aborting upload because only ${Object.keys(files).length} files were selected`)
    // show error message to the user
    uppy.info(`You have to select at least 2 files`, 'error', 500)
    return false
  }
}
1

There are 1 answers

0
Gino On BEST ANSWER

You need to make sure that the callback you posted is passed into your main uppy core initialization. It should work then as it is for me. Best of luck.

Hopefully, the code below can help you out.


  const Uppy = require('@uppy/core');

  let uppy = Uppy({
    autoProceed: false,
    allowMultipleUploads: true,
    logger: Uppy.debugLogger,
    restrictions: {
      maxNumberOfFiles: 15,
      maxFileSize: 10000000,
      minNumberOfFiles: 1,
      allowedFileTypes: ['image/*']
    },

    onBeforeFileAdded: (currentFile, files) => {
      const modifiedFile = {
        ...currentFile,
        name:  'yourfilename' + Date.now()
      }
      return modifiedFile
    }
  })