node-formidable / formidable

The most used, flexible, fast and streaming parser for multipart form data. Supports uploading to serverless environments, AWS S3, Azure, GCP or the filesystem. Used in production.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

discard file keeping the other form values

Benjamin-Urzua opened this issue · comments

Im trying to set the file.filepath to null when the file mimetype is not valid, and when i set the filepath to null the other form values are discarded too. Im doing it with form.on 'fileBegin'. Im doing this bc I create and update the object on the same site, but when I update, the user dont have to upload again the file. that makes the updated file object has no data, so when the user try to see de updated object he cant see it, bc the file has been rewrited and it has no data, obviusly the file always will have the same filename. Sorry about my english

For now im avoiding this sending the no data file to a trash directory, but obviously its not the best solution, it looks like this

  module.exports.crearProductos = (req, res) => {
  const form = new formidable.IncomingForm()
  const imgFolder = path.join(__dirname, "../public/img", "producto")

  var productos = []

  form.on('fileBegin', (formName, file) => {

      
      let numArchivo = formName.slice(-1)
      let nuevoArchivo = file
      let nombreArchivo = 'img_prod_0' + numArchivo

      if (file.mimetype == 'application/octet-stream') {
          nuevoArchivo.filepath = imgFolder + '/basura/' + nombreArchivo + '.png'
      }else{
          nuevoArchivo.filepath = imgFolder + '/' + nombreArchivo + '.png'
      }
      
      nuevoArchivo.newFilename = nombreArchivo
      form.emit('data', { name: 'fileBegin', value: nuevoArchivo });
  });

(My code is in spanish)

So you want to update something about a file, ? I suggest to handle that on a different URL but I am not sure I understand the question

So you want to update something about a file, ? I suggest to handle that on a different URL but I am not sure I understand the question

Uhm yeah, use another URL its probably the best way to do this, but my site is designed to create and update on the same place.

I'm using a plugin called Kartik Input File for this. The way this plugin works is that it allows for a more aesthetic way of uploading files. However, the problem I'm having is that even if the user decides not to upload anything, the plugin will still submit a file without any information in it. Additionally, the files will always have the same name for better data management. Specifically, the application focuses on creating products, which consist of a name, a link, a description, and a photo. Each of these fields has a similar naming convention such as txt_product_01 or img_product_02. If the product is product_01, the image will be called img_product_01. So every time the user updates this product, Formidable replaces the existing file. However, I only want this to happen when the user has actually uploaded a file. Otherwise, I would like to get rid of the file that comes without any data. But if I try to do this using the methods provided by Formidable, it will also get rid of the other fields that are accompanied by the file. For example, if the product is product_01 and its image comes without data, when trying to get rid of img_product_01, all the other fields, such as txt_productName_01, txt_linkProduct_01, and txt_descriptionProduct_01, will also be discarded. And this only happens when updating the product because the user may not always want to update the photo but may just want to update the product name.

I will think about it

Use option
allowEmptyFiles: false,

then check for the specific error
if (err) {
if (err.code ===formidableErrors.noEmptyFiles) {
update an item