【问题标题】:Uploading text with multer使用 multer 上传文本
【发布时间】:2019-05-16 03:02:47
【问题描述】:

所以我对编程还很陌生,如果我的代码混乱,我深表歉意。我想做的是建立一个人们可以上传消息和一些照片/视频的网站。这将用于我的婚礼,我希望能够将 URL 分发给人们,以便他们可以上传他们拍摄的所有照片和视频,并添加一条好消息。到目前为止,我有一个正在运行的服务器,它使用 multer、express 和 bodyParaser,我能够上传照片和视频,但不能上传在“textarea”中输入的文本。我错过了什么可以让某人在文本区域中写入的内容作为 .txt 文件上传到我的照片和视频所在的同一文件夹中?

我的文件树是这样设置的: Weddingupload_test

前端 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>

服务器端代码:

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}`);});

【问题讨论】:

  • 我知道这不一定在您上面概述的范围内,但是您是否考虑过引入像 Mongo 或 Firebase 这样的 nosql 数据库来存储每个条目?另一种选择是每次有人向您的服务器发帖时写入单个平面文件,并附上他们的消息——也许还有他们输入的名称。我对您最初的问题的预感是用户没有上传文本文件,他们正在发布文本,并且您希望将其保存为 .txt 文件。
  • @jonnycraze,我不知道 nosql、Mongo 或 Firebase。这可能是稍后尝试的选项。但你是对的,我希望将他们输入的内容更改为 .txt 文件。

标签: html node.js express multer body-parser


【解决方案1】:

首先将 value="text" 添加到 textarea。并且文本将在 req.body.text 上可用 然后我会使用“fs”模块来创建一个包含您收到的文本文件。

【讨论】:

  • 嘿内塔内尔,我将如何编写 fs 模块?我会使用“fs.appendFile()”吗?
【解决方案2】:
  • 编辑 index.html 这是 type="text" 的文本区域

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

  • 在 App.js 中需要 fs 和路径

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

  • 更新 multer 配置的目标函数以捕获文本并创建文本文件。

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'); }

【讨论】:

    猜你喜欢
    • 2019-07-31
    • 2020-12-31
    • 1970-01-01
    • 2020-09-04
    • 2016-03-09
    • 2018-07-21
    • 2017-01-13
    • 2018-08-12
    相关资源
    最近更新 更多