0.效果展示

1.创建项目

创建文件夹:express_file_upload

npm init

# 入口文件选择server.js
  • 安装插件
npm install express
npm install nodemon -g
npm install body-parser multer
npm install ejs
  • 参考资料

2.上传文件

  • 上传页面

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文件上传</title>
    </head>
    <form action="/upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" value="提交">
    </form>
    <body>
    </body>
    </html>
  • 路由

    // 上传页面
    router.get('/', (req, res)=>{
    console.log(__dirname)
    res.sendFile(path.join(__dirname,'../views/upload.html'))
    }) // 上传文件
    router.post('/upload', upload.single('file'), function(req, res) {
    console.dir(req.files) if (!req.file || Object.keys(req.file).length === 0) {
    res.status(400).send('请选择要上传的文件!');
    return;
    } // res.send('Success.');
    // 重定向到列表页
    res.redirect('/filelist')
    });

3.文件列表

  • 列表展示

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件列表</title>
    </head>
    <body>
    <h4>文件列表</h4>
    <ul id="filelist"></ul> <script src="/static/jquery.js"></script>
    <script>
    $(function(){
    init();
    }) function init(){
    $.ajax({
    type: 'GET',
    url:'/getFileList',
    success: function(data){
    console.log(data)
    $.each(data, function(index,item){
    $("#filelist").append("<li><a href='/download?path="+item.path+"'>"+
    item.name+"</a>&nbsp;&nbsp;&nbsp;&nbsp;"+getFileSize(item.size)+
    "&nbsp;&nbsp;&nbsp;&nbsp;<button οnclick='deleteFile(\""+item.path+"\")'>删除</button></li>");
    }) }
    })
    } function getFileSize(size){
    if(size < 1024){
    return size+'KB'
    }else if(size >= 1024&&size<Math.pow(1024, 3)){
    return (size/1024.0/1024).toFixed(2)+'MB'
    }else{
    return (size/1024.0/1024/1024).toFixed(2)+'GB'
    } } function deleteFile(path){
    var param={"path": path};
    console.log(path)
    if (confirm('确定删除?')){
    $.ajax({
    type: 'POST',
    url:'/delete?path='+path,
    data: JSON.stringify(param),
    success: function(data){
    window.location.reload();
    }
    })
    } return false; } </script>
    </body>
    </html>
  • 路由

    // 列表页面
    router.get('/filelist',function(req, res){
    res.sendFile(path.join(__dirname,'../views/filelist.html'))
    }) // 获取文件列表
    router.get('/getFileList',function(req, res, next){
    var filelist = getFileList('upload')
    res.send(filelist)
    }) function getFileList(path){
    var filelist = [];
    readFile(path, filelist); return filelist;
    } function readFile(path, filelist){
    var files = fs.readdirSync(path);
    files.forEach(walk); function walk(file)
    {
    var state = fs.statSync(path+'/'+file)
    if(state.isDirectory()){
    readFile(path+'/'+file, filelist)
    }else{
    var obj = new Object;
    obj.size = state.size;
    obj.name = file;
    obj.path = path+'/'+file;
    filelist.push(obj);
    }
    }
    }

4.下载文件

// 下载文件
router.get('/download',function(req,res){
var filePath = req.query.path;
console.log('下载文件:'+filePath)
filePath = path.join(__dirname,'../'+filePath);
res.attachment(filePath)
res.sendFile(filePath)
})

5.删除文件

// 删除文件
router.post('/delete', function(req, res, next){
var filePath = req.query.path;
console.log('删除文件:'+filePath) try {
fs.unlinkSync(filePath)
// 重定向到列表页
res.send('删除成功')
} catch (error) {
res.send('删除失败')
} })

源码地址

https://gitee.com/indexman/express_file_upload

路过的给老徐点个赞加个关注收藏:)

nodejs+express4实现文件上传下载删除和列表展示功能的更多相关文章

  1. nodejs+express-实现文件上传下载管理的网站

    Nodejs+Express-实现文件上传下载管理的网站 项目Github地址(对你有帮助记得给星哟):https://github.com/qcer/updo 后端:基于nodejs的express ...

  2. SpringMVC ajax技术无刷新文件上传下载删除示例

    参考 Spring MVC中上传文件实例 SpringMVC结合ajaxfileupload.js实现ajax无刷新文件上传 Spring MVC 文件上传下载 (FileOperateUtil.ja ...

  3. Struts2 文件上传,下载,删除

    本文介绍了: 1.基于表单的文件上传 2.Struts 2 的文件下载 3.Struts2.文件上传 4.使用FileInputStream FileOutputStream文件流来上传 5.使用Fi ...

  4. java操作FTP,实现文件上传下载删除操作

    上传文件到FTP服务器: /** * Description: 向FTP服务器上传文件 * @param url FTP服务器hostname * @param port FTP服务器端口,如果默认端 ...

  5. [java]文件上传下载删除与图片预览

    图片预览 @GetMapping("/image") @ResponseBody public Result image(@RequestParam("imageName ...

  6. Java 客户端操作 FastDFS 实现文件上传下载替换删除

    FastDFS 的作者余庆先生已经为我们开发好了 Java 对应的 SDK.这里需要解释一下:作者余庆并没有及时更新最新的 Java SDK 至 Maven 中央仓库,目前中央仓库最新版仍旧是 1.2 ...

  7. 艺萌TCP文件上传下载及自动更新系统介绍(TCP文件传输)(一)

    艺萌TCP文件上传下载及自动更新系统介绍(TCP文件传输) 该系统基于开源的networkComms通讯框架,此通讯框架以前是收费的,目前已经免费并开元,作者是英国的,开发时间5年多,框架很稳定. 项 ...

  8. 【FTP】FTP文件上传下载-支持断点续传

    Jar包:apache的commons-net包: 支持断点续传 支持进度监控(有时出不来,搞不清原因) 相关知识点 编码格式: UTF-8等; 文件类型: 包括[BINARY_FILE_TYPE(常 ...

  9. FastDFS实现文件上传下载实战

    正好,淘淘商城讲这一块的时候,我又想起来当时老徐让我写过一个关于实现FastDFS实现文件上传下载的使用文档,当时结合我们的ITOO的视频系统和毕业论文系统,整理了一下,有根据网上查到的知识,总结了一 ...

  10. JavaWeb实现文件上传下载功能实例解析

    转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web应用系统开发中,文件上传和下载功能是非常常用的功能 ...

随机推荐

  1. 幻兽帕鲁 Palworld 私有服务器一键部署教程

    <幻兽帕鲁>(日语:パルワールド,英语:Palworld) 是由日本开发商 Pocket Pair 推出的一款动作冒险生存游戏.游戏设定在一个由类似动物的生物 "帕鲁" ...

  2. [转帖]Oracle 23c 才支持 TLS1.3

    Transport Layer Security 1.3 Protocol Now Supported in Oracle Database Starting with Oracle Database ...

  3. VMto阿里云的简单过程

    VMto阿里云的简单过程 第一步打开网站 https://smcnext.console.aliyun.com/sourceServers/importMigrationSource?spm=5176 ...

  4. [转帖]关于面试时HA(RAC)会问到的一些问题

    1.什么是RAC(Real Application Cluster)? RAC(Real Application Cluster)是Oracle数据库的一种部署架构,它将多个数据库服务器连接在一起,共 ...

  5. [转帖]12.24.2 DECIMAL Data Type Characteristics

    https://dev.mysql.com/doc/refman/8.0/en/fixed-point-types.html This section discusses the characteri ...

  6. [转帖]Rocksdb的优劣及应用场景分析

      研究Rocksdb已经有七个月的时间了,这期间阅读了它的大部分代码,对底层存储引擎进行了适配,同时也做了大量的测试.在正式研究之前由于对其在本地存储引擎这个江湖地位的膜拜,把它想象的很完美,深入摸 ...

  7. 【转帖】Linux中如何取消ln链接?(linuxln取消)

    https://www.dbs724.com/163754.html Linux系统使用ln命令可以快速创建链接,ln链接是指把文件和目录链接起来,当改变源时可以快速地改变整个目录下的文件和目录.有时 ...

  8. [转帖]字节跳动开源 Shmipc:基于共享内存的高性能 IPC

    https://maimai.cn/article/detail?fid=1780832041&efid=WeW8ji-LiPaXA8QER_Q1YQ 简介 CloudWeGo - Shmip ...

  9. [转帖]宁可信鬼,也不信 iowait 这张嘴!

    https://zhuanlan.zhihu.com/p/407333624 原创:小姐姐味道(微信公众号ID:xjjdog),欢迎分享,转载请保留出处. 我们经常遇到iowait这个名词,在top命 ...

  10. chrome谷歌浏览器多开(独立环境 独立cookie)

    复制下面代码(路径) "C:\Program Files\Google\Chrome\Application\chrome.exe" --user-data-dir="D ...