0

Folder Structure image# Multer.js File

const multer = require("multer");
const path = require("path");
const fs = require("fs");
const httpStatus = require("http-status");

const ApiError = require("../utils/ApiError")
const logger = require("../utils/logger");

const multerUpload = async (req, res, next) => {
    let fileName = "";
    let storage = multer.diskStorage({
        destination: function (req, file, callback) {
            fs.mkdir(path.join(path.resolve(), "/tmp"), (err) => {
                if (err) {
                    logger.error("mkdir tmp %o", err);
                }
                callback(null, path.join(path.resolve(), "/tmp"));
            });
        },
        filename: function (req, file, callback) {
            fileName = file.fieldname + "-" + req.query.eventId + Date.now() + path.extname(file.originalname);
            logger.info("filename of uploadSheet===> %s", fileName);
            callback(null, fileName);
        },
    });

    // below code is to read the added data to DB from file
    var upload = multer({
        storage: storage,
        fileFilter: function (req, file, callback) {
            var ext = path.extname(file.originalname);
            if (ext !== '.xlsx') {
                return callback(new Error('Only Excel sheets are allowed'))
            }
            callback(null, true)
        },
    }).single("sheet");
    upload(req, res, async function (err) {
        if (err) {
            next(new ApiError(httpStatus.INTERNAL_SERVER_ERROR, err.message));
        } else {
            req.fileName = fileName;
            next();
        }
    })
}
module.exports = multerUpload;

It gives error of EORFS read only file in vercel production but the code works fine in local.

I'm trying to upload the excel sheet file from the Api and then read the data from it and add it into the Mongodb.

  • Some more details on "It gives error of EORFS read only file in vercel production" might be nice.... – Luuk Dec 26 '22 at 12:34

2 Answers2

0

I once encountered this same problem working with Heroku a long time ago, I haven't worked with vercel but with quick research, I will say this is the cause, vercel does not provide storage for you to upload files to in production, you need a separate service for that like Amazon S3, but there also exists Azure File Storage and Google Cloud Storage.

alternatively, if you don't want to add more services to your project, you can just convert the image to base64 string and save it as text(but need to make the field/column read-only so it does not get corrupted) NOT the best alternative but it was something I once did

Sammy
  • 99
  • 4
0

To use /tmp in server functions, you should just use /tmp/your-file. Remove path.resolve().

Only if you need to store something temporarily, you may try to use /tmp directory.

Limit 512 MB + no guaranty - https://github.com/vercel/vercel/discussions/5320

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122