首页 > 编程语言 >node+express+ multer 实现文件上传入门

node+express+ multer 实现文件上传入门

时间:2023-01-23 13:33:00浏览次数:49  
标签:node 文件 express upload multer file var 上传

文件上传
文件上传需要借助一个中间件 multer 
因此我们需要安装 cnpm install multer --save
前端界面
在express创建的项目下的 public/upload目录下创建 indexfileupload.html
如果你没有需要进行创建对应的文件
然后再地址栏输出 http://127.0.0.1:666/upload/indexfileupload.html
可以显示下下面对应的界面

node+express+ multer 实现文件上传入门_上传

indexfileupload.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文件上传</title>
</head>

<body>
<form action="/upload/upload" method="post" enctype="multipart/form-data">
<input type="file" name="logo2"> <br />
<input type="submit" value="文件上传">
</form>
</body>
</html>
indexfileupload.html文件的注意点
1===》action="/upload/upload" 表示的是接口地址。
因为这里前后端没有分离,因此不需要写最前面的地址

2==》method="post" 表示的是请求的方式

3==》enctype="multipart/form-data" 文件上传的固定格式

4==》<input type="file" name="logo2"> 中的file表示文件。
name="logo2" 需要与 后端的 upload.single('name') 与之对应

router.post('/upload', upload.single('logo2'), (req,res) => {
var file = req.file
res.send({ code:'0', msg:'上传成功'})
})
后端代码-在routes目录下创建 fileupload.js文件
var express = require('express')
var router = express.Router()
// 引入文件模块
var fs = require('fs')
// 引入路径模块
var path = require('path')

// 后端处理文件的模块
var multer=require('multer')

// 配置路径和文件名
var storage = multer.diskStorage({
//上传文件到服务器的存储位置
destination: 'public/upload',
filename: function (req, file, callback) {
console.log('file', file) //上传的文件信息
var fileFormat = (file.originalname).split('.')
var filename = new Date().getTime()
callback(null, filename+"."+fileFormat[fileFormat.length-1])
}
})

var upload = multer({
storage
})

//接口
router.post('/upload', upload.single('logo2'), (req,res) => {
var file = req.file
res.send({ code:'0', msg:'上传成功'})
})

//导出
module.exports = router;
在app.js中注册使用路由
// 引入
var uploadRouter = require('./routes/fileupload');
//使用
app.use('/upload', uploadRouter);
在浏览器中输入http://127.0.0.1:666/upload/indexfileupload.html
然后选择对应的文件,然后点击文件上传按钮,就可以实现文件上传成功了

node+express+ multer 实现文件上传入门_html_02

遇见问题,这是你成长的机会,如果你能够解决,这就是收获。


作者:​​晚来南风晚相识​​



标签:node,文件,express,upload,multer,file,var,上传
From: https://blog.51cto.com/u_12553473/6021906

相关文章

  • sql base nodejs py go操作基本的db
    constmysql=require('mysql2');constconnection=mysql.createConnection({host:'localhost',user:'root',password:'root',database:'mybatis_pl......
  • javascript: node.js
     consthttp=require("http");http.createServer(function(request,response){response.writeHead(200,{'Content-type':'text/html'});response.end('<h1>......
  • vscode 使用 ts-node 调试当前文件
    launch.json内configurations添加如下内容{ "name":"当前ts文件", "type":"node", "request":"launch", "program":"${workspaceRoot}/node_modules/ts-node......
  • C# 使用DevExpress轻松将SVG转为Imag格式
    如果你正在使用DevExpress,那么轻松搞定!//从资源中导入SVG图片SvgBitmapsvgImage=newSvgBitmap(Resources.svgImg);Imageimg1=svgImage.Render(null,1);//1是缩......
  • node.js安装
    node.js安装1.官网下载安装包下载地址:https://nodejs.org/en/download/根据自己的电脑系统选择对应的安装包,由于我用的是windows电脑(64位),下载这个安装包,是一个.msi文......
  • 关于使用express报错:Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they
    浏览器中首次能够访问{"code":200,"data":[ { "url":"https://www.douyin.com" }],"message":"请求成功!"}第二次访问就无法访问了......
  • 使用 IDEA 工具打开vue/react/node项目
    1.IDEA在官网上下载社区免费版的https://www.jetbrains.com/zh-cn/idea/download/#section=mac下载完成后,一直点击next安装即可2.安装完成后点击open打开gitcl......
  • node+express+ multer 实现文件上传入门
    文件上传文件上传需要借助一个中间件multer因此我们需要安装cnpminstallmulter--save前端界面在express创建的项目下的public/upload目录下创建indexfileuplo......
  • JavaScript精简(基于node.js)
    目录1、基本语法2、变量3、数据类型3.1、数字型Number3.2、字符串类型String3.3、布尔型Boolean3.4、Undefined、Null3.5、获取变量的数据类型3.6、数据类型的转化4、运......
  • nodejs 导出excel
    window.export=function(){layer.msg(MOD_PAGE_PATH+'/export');window.location=MOD_PAGE_PATH+'/export'......