代码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();
})