nodejs multer文件上传,发生的事情文件名在2个地方是相同的 [英] nodejs multer file upload, what happens file names are the same from 2 places

查看:461
本文介绍了nodejs multer文件上传,发生的事情文件名在2个地方是相同的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个概念对我来说还不清楚. 对于一个非常基本的情况,我可以使用multer上传文件(例如myFile.txt),并将其原始名称保存在服务器端.

The concept is still not clear to me yet. For a very basic case, i can upload a file (say myFile.txt) using multer, and keep its original name in the server side.

const upload = multer({ dest: `${UPLOAD_PATH}/` }); // multer configuration

现在,另一个人碰巧将另一个具有相同文件名myFile.txt的文件上传到服务器端的同一文件夹中.它会覆盖前一个吗?

Now another person happen to upload a different file with same file name myFile.txt to the same folder in server side. Will it overwrite the previous one ?

您通常如何管理?谢谢!

How do you normally manage this ? Thanks !

推荐答案

它会覆盖上一个吗?

Will it overwrite the previous one ?

是的,肯定会用新的替换.这是我的代码.在此代码中,如果您从不同位置使用相同文件名,或者从相同位置使用该文件名将不会替换该文件,这会将两个文件都保留在目标位置.服务器代码server.js

Yes it will definitely replace with the new one.Here is my code .In this code if you use a same file name from different locations or from the same location it won't replaces.It keeps both the files in destination.Server code server.js

var express=require('express');
var multer=require('multer');
var path = require('path')
var app=express();
var ejs = require('ejs')
app.set('view engine', 'ejs')
var storage = multer.diskStorage({
    destination: function(req, file, callback) {
        callback(null, './public/uploads')
    },
    filename: function(req, file, callback) {
        callback(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname))
        //callback(null, file.originalname)
    }
})

app.get('/api/file',function(req,res){
res.render('index');
});
app.post('/api/file', function(req, res) {
    var upload = multer({
        storage: storage}).single('userFile');
    upload(req, res, function(err) {
        console.log("File uploaded");
        res.end('File is uploaded')
    })
})

app.listen(3000,function(){
console.log("working on port 3000");
});

如果您观察到代码callback(null, file.originalname),则此行会将原始文件名保留在目标位置,并在文件名相同时替换该文件.如果您不希望这样做,请使用callback(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname)).此回调更改目标中的文件名.

If you observe the code callback(null, file.originalname) this line will keep the original file name in destination and will replace the file when it gets the same name.If you don't want this then use callback(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname)).This callback changes the name of file in destination.

创建一个views文件夹并将其保存在其中.您可以从ejs代码中选择要上传的文件index.ejs

Make a views folder and keep this file in it.ejs code from which you can choose a file to upload index.ejs

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <form id="uploadForm" enctype="multipart/form-data" method="post">
        <input type="file" name="userFile" />
        <input type="submit" value="Upload File" name="submit">
    </form>
</body>
</html>

在节点server.js上运行代码.打开浏览器并键入http://localhost:3000/api/file.选择要上传的文件并查看目标文件夹.希望这对您有所帮助.

Run the code as node server.js.Open the browser and type http://localhost:3000/api/file.Choose a file to upload and see the destination folder.Hope this helps for you.

这篇关于nodejs multer文件上传,发生的事情文件名在2个地方是相同的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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