node.js使用multer搭建图片接收服务器

node服务端 server.js

var express = require('express'),
    multer = require('multer');

var app = express();
var storge = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, 'uploads')
    },
    filename: function (req, file, cb) {
        var fileformat = (file.originalname).split('.');
        cb(null, file.fieldname+'-'+Date.now()+'.'+fileformat[fileformat.length-1]);
    }
})

var upload = multer({storage: storge})
app.use(express.static('./static'));

app.post('/', upload.array('file',20), function (req, res, next) {
   console.log(req.files);
    res.send('abc');
})

app.listen(3000)
console.log('服务开启成功--127.0.0.1:3000')

微信小程序前端

js

let app = getApp();
Page({
  data: {
  },
  upload: function () {
    wx.chooseImage({
      count: 1,  //选择图片的数量 默认为9
      sizeType: ['original', 'compressed'],  //原图,缩略图
      sourceType: ['album', 'camera'],  //相册,摄像头
      success: (res) => {
        const tempFilePaths = res.tempFilePaths[0]
        this.setData({
          coverImg: tempFilePaths  //选定照片的本地文件路径,对应  
        });
        this.coverImg = tempFilePaths;

        wx.uploadFile({
          url: 'http://127.0.0.1:3000', //服务器接口地址
          filePath: tempFilePaths,
          name: 'file',
          success: function (res) {
            console.log(res);
          },
          fail: function (e) {
            console.log(e.stack);
          }
        })
      }
    })
  } 
})  

wxml



 
                    

wxss

.imgbox{
  width:90%;
  height:450rpx;
  border:1px solid rgb(120, 153, 153);
  border-radius:2px;
  box-shadow: 10px 10px 5px #888888;
  margin:30rpx;
}
.img{
  width:90%;
  height:450rpx;  
}

效果图
node.js使用multer搭建图片接收服务器_第1张图片

你可能感兴趣的:(nodejs)