Home > database >  Upload .txt file in Node.js
Upload .txt file in Node.js

Time:10-07

I want to upload files for multiple fields (like html_template and preview_image fields) but Node.js is not accepting it and more over it does not logs any error in the console but in the postman it responds with internal server error.

The multer function:

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, "src/uploads/");
  },
  filename: function (req, file, cb) {
    cb(
      null,
      file.fieldname   "-"   Date.now()   path.extname(file.originalname)
    );
  },
});

var upload = multer({
  storage: storage,
  fileFilter: function (req, file, cb) {
    var filetypes = /jpeg|jpg|png|pdf|txt/;
    var mimetype = filetypes.test(file.mimetype);
    var extname = filetypes.test(path.extname(file.originalname).toLowerCase());
    if (mimetype && extname) {
      return cb(null, true);
    }
    cb("Please upload valid file");
  },
});

The create template route:

router
  .route("/create")
  .post(
    upload.fields([{ name: "html_template" }, { name: "preview_image" }]),
    Template.createTemplate
  );

If I remove the field { name: "html_template" } from the route then it works fine but it does not work with this field html_template

The templateCreator controller function:

exports.createTemplate = catchAsync(async (req, res) => {
  try {
    console.log(req.files);
    const template = await templateService.createTemplate(req);
    return res.succeed(template, "Template created successfully");
  } catch (error) {
    console.trace(error);
    return res.failed(500, "Internal Server Error", error);
  }
});

The service function:

exports.createTemplate = async (req) => {
  const name = req.body.name;
  const html_template = req.files.html_template;
  const preview_image = req.files.preview_image;

  const imagePath = preview_image.map((image) => image.path);

  const template = new Template({
    name,
    html_template,
    preview_image: imagePath.toString(),
  });
  await template.save();
  return template;
};

I have tried upload.fields and upload.any but it just doesn't work. I am unable to figure out why.

If I send the jpg, jpeg, png file then it accepts it and saves it to the database but not the .txt file. Why is that?

CodePudding user response:

Because the MIME type for plain text files is text/plain. It doesn't contain the string txt.

It'd be better to list the full MIME types that you accept instead of doing a substring match.

The extension shouldn't really matter at that point, but if you want it to matter, you have to check it separately.

CodePudding user response:

You forget to define a list of MIME types to test your file mime type with it. I edit your fileFilter function like below and it works.

fileFilter: function (req, file, cb) {
var filetypes = /jpeg|jpg|png|pdf|txt/;
var mimeTypes = /text\/plain|image\/jpeg|image\/jpg|image\/png|application\/pdf/;
var mimetype = mimeTypes.test(file.mimetype);
var extname = filetypes.test(path.extname(file.originalname).toLowerCase());
if (mimetype && extname) {
  return cb(null, true);
}
cb("Please upload valid file");

}

  • Related