1

I'm developing a simple software college project that needs pdf/doc file upload. But here comes the bottleneck: I couldn't find anywhere an example and example of this feature using the Sequelize ORM.

Has anyone done something similar using this framework?

*By the way, I know there are several npm packages for express(), but I must use sequelize.

Any advice is welcome.

Thanks in advance ;)

2 Answers 2

5

Configure your express app with multer. Read over the documentation for multer, but in short you store the path of the uploaded file:

const multer = require('multer')
const express = require('express')
const Sequelize = require('sequelize')
const sequelize = new Sequelize('database', 'username', 'password')

const MyModel = sequelize.define('myModel', {
  filePath: Sequelize.STRING,
})

const express = express()

const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, './app/uploads')
  },
  filename: (req, file, cb) => {
    cb(null, file.originalname)
  }
})

app.post('/upload', multer({ storage }).single('example'), async (req, res) => {
    // This needs to be done elsewhere. For this example we do it here.
    await sequelize.sync()

    const filePath = `${req.file.destination}/${req.file.filename}`
    const myModel = await MyModel.create({ filePath })
})
Sign up to request clarification or add additional context in comments.

Comments

0

A slightly simpler example (from) using AJAX.

Add to your node.js

var multer = require('multer');

const storage = multer.diskStorage({
    destination: (req, file, callback) => {
        console.log(req);
        callback(null, './uploads');
    },
    filename: (req, file, callback) => {
        console.log(req);
        callback(null, Date.now() + file.originalname);
    }
});

var upload = multer({storage:storage}).single('myFile');

app.post('/dashboard/myFile', function(req,res){
        upload(req,res,function(err){
            //console.log("owen",req.file,err);
            if (err)
                return res.end("error uploading file");
            res.end("file is uploaded");
        });
});

And in your HTML

<form id="myForm" name="myForm" enctype="multipart/form-data" method="post">
    <input id="myFile" name="myFile" type="file">
    <button type="submit" class="btn btn-primary">Submit</button>
</form>


<script>

var form = document.forms.namedItem("myForm");

form.addEventListener('submit', function(ev){
    var myFile = document.getElementById('myFile').files[0];
    var oData = new FormData(form);
    var oReq = new XMLHttpRequest();
    oReq.open("POST","/uploadFile",true);
    oReq.onload = function(oEvent){
        if(oReq.status == 200) {
            console.log("success",oEvent);
        } else {
            console.log("fail",oEvent);
        }
    }
    oReq.send(oData);
    ev.preventDefault();
},false);

</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.