文件上传下载
程序员文章站
2022-04-14 08:55:57
文件下载和文件上传上传//前台 realgo(url){ // 向后台发送文件 let data = new FormData(); data.append('url',url); axios.post('/user/upload',data, {headers: {'Content-Type': 'multipart/form-data'}}).then(res=>{ console.log(res)...
文件下载和文件上传
上传
//前台
realgo(url){
// 向后台发送文件
let data = new FormData();
data.append('url',url);
axios.post('/user/upload',data, {headers: {'Content-Type': 'multipart/form-data'}}).then(res=>{
console.log(res)
this.download=res.data.url;
this.phone=res.data.num;
})
},
//后台
let uploadAvatar = (req,res)=>{
let str = JSON.stringify(req.body);
str = JSON.parse(str);
console.log(str.url)
let num ='';
for(var i = 1;i <= 6;i++){
const code = Math.floor(Math.random()*10);
num+= code;
}
let rStream = fs.createReadStream(str.url);
let word=str.url.slice(str.url.indexOf('.'))
console.log(word)
let wStream = fs.createWriteStream(path.join(__dirname, '../../public/avatar/' + './'+num+word));
rStream.pipe(wStream)
res.send({code:1,url:'/avatar/'+num+word,num})
}
下载文件
window.location.href='http://192.168.43.82:3000'+download;
ps:不需要别的后台代码,必须保证后台的静态资源路径正确
本文地址:https://blog.csdn.net/yuanmengfanseven/article/details/108991037