关于图片上传到七牛?
来源:7-6 Vue 中实现上传宝贝图片到七牛
拖车板牙爵士
2017-09-05
迫切的希望老师能写一篇手记,能讲述下如何实现图片上传到七牛的思路和实现过程?
我在图片上传到七牛这个坑里待了一个多月了,大神拉我一把,死的心都有。
如果你没空写,那么指点我写,我是这样写的,应该改成什么样子
后端
const qiniu = require('qiniu'); // 本地文件路径 qiniu.conf.ACCESS_KEY = ''; qiniu.conf.SECRET_KEY = ''; exports.upload = function(req,res) { const uptoken = (bucket, key) => { let putPolicy = new qiniu.rs.PutPolicy(bucket + ':' + key); return putPolicy.token(); } const uploadFile = (uptoken, key, localfile) => { qiniu.io.putFile(uptoken, key, localFile, extra, function(err, ret) { if(!err) { // 上传成功, 处理返回值 return res.json({ hash: ret.hash, key: ret.key, per: ret.persistentId }); } else { // 上传失败, 处理返回代码 console.log(err); } }); } const bucket = 'ipearling'; const key = req.body.key; const token = uptoken(bucket,key,req.files[0].path) uploadFile(token,key,req.files[index].path) }
前端
<template> <div id="image"> <el-upload action="https://ovj5zotk8.bkt.clouddn.com" :data="dataObj" drag :multiple="true" :before-upload="beforeUpload" :on-success='handleSuccess'> <i class="el-icon-upload"></i> <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div> </el-upload> </div> </template> <script type="text/ecmascript-6"> export default{ data() { return { dataObj: { token: '', key: '' }, image_uri: [], fileList: [] }; }, methods: { beforeUpload (file) { const tath = this; const name = file.name; const curr = Date.now().toString(); const key = encodeURI(`${curr}_${name}`); return new Promise((resolve, reject) => { this.axios.post('/api/image', { key }).then(res => { console.log(res.data); tath.data.dataObj.token = res.data.token; tath.data.dataObj.key = res.data.key; resolve(true); }).catch(err => { console.log(err); reject(false); }); }); }, handleSuccess() { console.log('上传成功'); } } }; </script>
写回答
1回答
-
汐汐
2017-09-05
00
相似问题