Search code examples
htmlnode.jsexpressmulterbody-parser

Uploading text with multer


So i'm fairly new to programming and I apologize if my code is messy. What I am trying to do is make a website that people can upload a message and some photos/videos to. This is going to be used for my wedding and I want to be able to hand out the URL to people so that they can upload all the photos and videos they took, plus add a nice message. So far I have a running server that uses multer, express, and bodyParaser and i'm able to upload photos and videos but not the text that is typed in the "textarea". What am I missing that would allow what ever someone writes in the text area to be uploaded as a .txt file to the same folder that my photos and videos go into?

My file tree is set up like this: Weddingupload_test

Front end HTML

            <form style="text-align: center;" action="/upload" enctype="multipart/form-data" method="POST">

                <textarea class="textBox" name="message" rows="10" cols="50" placeholder="Share your favorite moment..."></textarea>

                <div id=uploadBtn>
                    <label class="uploadBtn">
                        <input type="file" id="photo" name="photo" multiple accept="image/*,video/*,audio/*" />
                        Attach Images
                    </label>
                </div>

                <div id="submitBtn">
                    <input class="submitBtn" type="submit" name="Upload" value="Upload Photo" />
                </div>
            </form>

Server Side Code:

const express = require('express');
const multer = require('multer');
const bodyParser = require('body-parser');

const app = express();
const port = process.env.PORT || 3000;
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
app.use('/', express.static(__dirname + '/public'));

const multerConfig = {
  storage: multer.diskStorage({
   destination: function(req, file, next){
      next(null, './uploads/photos');
    },
     filename: function(req, file, next){
     console.log(file);
     const ext = file.mimetype.split('/')[1];
     next(null, file.fieldname + '-' + Date.now() + '.'+ext);
   }
 })
};

app.get('/', function(req, res){
res.render('index.html');
   });

app.post('/upload', multer(multerConfig).array('photo'),function(req, res){
res.sendFile('public/second.html', {root: __dirname })
});

app.listen(port,function(){
console.log(`Server listening on port ${port}`);});

Solution

    • edit index.html this is the text area with type="text"

    <textarea type="text" class="textBox" name="message" rows="10" cols="50" placeholder="Share your favorite moment..."></textarea>

    • in App.js require fs and path

    const fs = require('fs'); const path = require('path');

    • update the destination function of the multer config to capture the text and create a text file.

    destination: function (req, file, next) { let text = req.body.message; let now = Date.now(); fs.writeFile(path.join(__dirname, './uploads/' + file.originalname + '-' + now + '.txt'), text, console.log); next(null, './uploads'); }