Multer不会将文件保存为相同名称且没有扩展名吗? [英] Multer is not saving the file as the same name and without extension?

查看:106
本文介绍了Multer不会将文件保存为相同名称且没有扩展名吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Multer 保存通过表单上传的文件,但我不知道为什么我的代码将其另存为一个奇怪的名称并且没有扩展名,而我只是使用了文档中的代码.

server.js:

const multer  = require('multer');
const app = express();
var upload = multer({ dest: 'uploads/' })

app.post('/file', upload.single('filesToAttach'), function (req, res, next) {
    console.log(req.file);
    loadUserPage(req, res);
})

userPage.ejs:

<form action="/file" method="post" enctype="multipart/form-data">
    <div id="frm-attachments">
        <div>
            <h3>Attachments</h3>
            <div>
                <input type="file" id="attachFiles" name="filesToAttach" />
                <input type="submit" value="Attach">
            </div>
            <div id="frm-attach-files">
                Attached files
                <div>
                    <textarea id="field-attached-files" class="large-textbox" name="attached-files" spellcheck="true" rows="10" cols="50" tabindex="4" disabled="true"></textarea>
                </div>
            </div>
        </div>
    </div>
</form>

当我单击提交"按钮时,文件夹uploads中将出现一个新文件,该文件应具有与我在表单中上载的文件相同的名称和扩展名,但是它具有以下名称:

如果我尝试打印出(req.file),我会看到:

为什么会这样?我什至不明白他们为什么在文档中写错了代码...

解决方案

您可以在外部进行设置,

尝试一下,

const multer  = require('multer');
const app = express();
var storage = multer.diskStorage({
  destination: 'uploads/',
  filename: function(req, file, callback) {
    callback(null, file.originalname);
  }
});
var upload = multer({ storage: storage })

app.post('/file', upload.single('filesToAttach'), function (req, res, next) {
    console.log(req.file);
    loadUserPage(req, res);
})

I am using Multer to save files I upload through a form but I do not know why my code saves it as a weird name and without extension and I have just used the code from the documentation.

server.js:

const multer  = require('multer');
const app = express();
var upload = multer({ dest: 'uploads/' })

app.post('/file', upload.single('filesToAttach'), function (req, res, next) {
    console.log(req.file);
    loadUserPage(req, res);
})

userPage.ejs:

<form action="/file" method="post" enctype="multipart/form-data">
    <div id="frm-attachments">
        <div>
            <h3>Attachments</h3>
            <div>
                <input type="file" id="attachFiles" name="filesToAttach" />
                <input type="submit" value="Attach">
            </div>
            <div id="frm-attach-files">
                Attached files
                <div>
                    <textarea id="field-attached-files" class="large-textbox" name="attached-files" spellcheck="true" rows="10" cols="50" tabindex="4" disabled="true"></textarea>
                </div>
            </div>
        </div>
    </div>
</form>

When I click on the submit button, a new file appear in the folder uploads which is supposed to have the same name and same extension as the file I uploaded in the form, but it has this name instead:

And if I try to print out(req.file), I see this:

Why is this happening? I do not even understand why they write the wrong code in the documentation...

解决方案

You can set it externally,

Try this,

const multer  = require('multer');
const app = express();
var storage = multer.diskStorage({
  destination: 'uploads/',
  filename: function(req, file, callback) {
    callback(null, file.originalname);
  }
});
var upload = multer({ storage: storage })

app.post('/file', upload.single('filesToAttach'), function (req, res, next) {
    console.log(req.file);
    loadUserPage(req, res);
})

这篇关于Multer不会将文件保存为相同名称且没有扩展名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆