微信小程序拍照上传(nodejs)

代码js

const w=this
wx.showActionSheet({
  itemList: ['相册','相机'],//对应0相册 1相机
  success:function(res){
    console.log(res)
    if(res.tapIndex==0||res.tapIndex==1){
      wx.chooseImage({//选择照片
        complete: (res) => {},
        success:function(res){
          console.log(res)//返回临时文件信息
      const a=wx.uploadFile({//上传文件路径
            filePath: res.tempFilePaths[0],
            name: 'img',
            url: 'https://25q686n876.qicp.vip/file',
            formData:{file:"123"},//附加信息与formdata对象一样append
            success:function(res){
              console.log(res.data)
            }
          })
      a.onProgressUpdate((res)=>{console.log(res)})//后台上传观察进度
              w.setData({//获取临时路径 然后image组件预览
                src:res.tempFilePaths[0]
              })
        }
      })
    }
  }
})

nodejs代码
const multer = require(‘koa-multer’);

//以下是配置

var storage = multer.diskStorage({
	//定义文件保存路径
	destination:function(req,file,cb){
	    cb(null,'D:/koa/static/uploads/');//路径根据具体而定。如果不存在的话会自动创建一个路径
	},                       //注意这里有个,
	//修改文件名
	filename:function(req,file,cb){
	    var fileFormat = (file.originalname).split(".");
          //  cb(null,Date.now() + "." + fileFormat[fileFormat.length - 1]);
          cb(null,file.originalname)//微信路径是一串乱码 如果是web就可以得到文件名
          console.log(file)
          console.log(req.body.file)
    },
    
})

路由配置

router.post("/file",upload.single('img'),async (ctx)=>{
    function file(){
        return new Promise((resovle,rejects)=>{
                resovle({fil:2})
                console.log(ctx.req.body.file)
        })
    }
    ctx.body=await file();
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值