let OSS = require('ali-oss');
let config = {
region: 'oss-cn-hangzhou', //你的Region 注意 这个只要 空间名 不要 http:// 和 .aliyunoss.com !!
//secure: true,
accessKeyId: XXXX,//你的accessKeyId
accessKeySecret: XXXX,//你的accessKeySecret
bucket: 'ipb'
}; /**
* 配置
*/
let init = () => {
return new OSS(config);
} /**
* 生成uuid
*/
let guid = () => {
let S4 = () => { return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
} /**
* 修改文件名字
*/
let fileName = (file,key) => {
let arr = file.name.split(".");
var uuid = guid();
if (arr.length > 1) {
return key + uuid + '.' + arr[arr.length - 1];
} else {
return uuid;
}
} /**
* 上传文件
*/
apis.getOssToken({
roleSessionName: 123
})
.then(res => {
console.log(res) }) let ossPut = (file,key) => {
return new Promise((resolve, reject) => {
let objectName = fileName(file,key);
init().put(objectName, file).then(({
res,
url
}) => {
if (res && res.status == 200) {
console.log('阿里云OSS上传文件成功回调', res, url, objectName); let data = {
res: res,
url: url,
objectName: objectName
};
resolve(data);
}
}).catch((err) => {
console.log('阿里云OSS上传文件失败回调', err);
reject(err)
});
})
} /**
* 下载文件
*/
let ossGet = (name) => { return new Promise((resolve, reject) => {
let signUrl = init().signatureUrl(name, {expires: 300});
resolve(signUrl);
})
} let ossDel = (name) => {
return new Promise((resolve, reject) => {
init().delete(name).then((res) => {
if (res && res.status == 200) {
console.log('阿里云OSS删除文件成功回调', res);
resolve(res);
}
}).catch((err) => {
console.log('阿里云OSS删除文件失败回调', err);
reject(err)
});
})
} export default {
ossPut,
ossGet,
ossDel
}

调用

<el-form-item label="头像" class="user">
<el-input type="hidden"></el-input>
<el-upload class="iptImg" ref="uploadAvatar" action list-type="picture-card" :http-request="uploadImg"
:before-upload="beforeUploadImg" :on-remove="removeFileForm" :on-exceed="exceedFileForm">
<img v-if="addForm.url" :src="addForm.url" class="avatarDis" />
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
</el-form-item> <script>
export default {
data() {
return {
addForm: {
avatar: "",
url:"",
}
}
}, methods: {
//图片类型验证
beforeUploadImg(file) {
const isJPG = file.type === "image/jpg";
const isJPEG = file.type === "image/jpeg";
const isPNG = file.type === "image/png";
const isGIF = file.type === "image/gif";
const isBMP = file.type === "image/bmp";
if (!isJPG && !isJPEG && !isPNG && !isGIF && !isBMP) {
this.$message.error("只能上传图片格式为jpg,jpeg,png,gif,bmp!");
}
return isJPG || isJPEG || isPNG || isGIF || isBMP;
}, //点击图片上传
uploadImg(file) {
// this.addForm.file.push(file.file);
// console.log(this.addForm.file);
if(this.addForm.avatar != "" && this.addForm.avatar != null){
this.removeFileForm(file);
}
this.$ossClient
.ossPut(file.file, "PartyMember/")
.then(res => {
this.addForm.avatar = res.objectName;
})
.catch(e => {
console.log(e);
});
}, //移除文件
removeFileForm(file) {
// this.addForm.file.splice(this.addForm.file.indexOf(file.raw), 1);
// console.log(this.addForm.file);
this.$ossClient
.ossDel(this.addForm.avatar)
.then(res => {
this.addForm.avatar = "";
})
.catch(e => {
console.log(e);
});
},
//超出数量限制
exceedFileForm() {
this.$message.error("最多上传1个头像");
},
//数据回显
getPartyInfo() {
this.partyId = this.$route.query.partyId;
axios({
method: "get", //请求方式
url: "/api/commonPartyMember/getPartyMember", //请求地址
params: {
id: this.partyId
}
})
.then(res => {
this.addForm.avatar = res.data.data.avatar;
if (this.addForm.avatar != "" && this.addForm.avatar != null) {
this.$ossClient
.ossGet(this.addForm.avatar)
.then(res => {
this.addForm.url = res;
})
.catch(e => {
console.log(e);
});
} })
.catch(err => {
console.log(err);
});
}, }, } </script>

OSS管理文件(Node.js)的更多相关文章

  1. 封装读取文件(node js)

    我们都会简单的读取文件,今天我们就来讲一下用函数封装读取文件. 1.首先我们要先建好文件 2.我们在index.js里面写入代码: var http=require('http'); var fs=r ...

  2. Node.js NPM 管理包

    章节 Node.js NPM 介绍 Node.js NPM 作用 Node.js NPM 包(Package) Node.js NPM 管理包 Node.js NPM Package.json 根据安 ...

  3. rtags——node.js+redis实现的标签管理模块

    引言在我们游览网页时,随处可见标签的身影: 进入个人微博主页,可以看到自己/他人的标签,微博系统会推送与你有相同标签的人 游览博文,大多数博文有标签标记,以说明文章主旨,方便搜索和查阅 网上购物,我们 ...

  4. Node.js模块

    每一个Node.js都是一个Node.js模块,包括JavaScript文件(.js).JSON文本文件(.json)和二进制模块文件(.node). mymodul.js function Hell ...

  5. Node.js快速入门

    Node.js是什么? Node.js是建立在谷歌Chrome的JavaScript引擎(V8引擎)的Web应用程序框架. 它的最新版本是:v0.12.7(在编写本教程时的版本).Node.js在官方 ...

  6. Node.js开发——MongoDB与Mongoose

    为了保存网站的用户数据和业务数据,通常需要一个数据库.MongoDB和Node.js特别般配,因为MongoDB是基于文档的非关系型数据库,文档是按BSON(JSON的轻量化二进制格式)存储的,增删改 ...

  7. Node.js学习(Node.js基础)

    1.开发环境搭建 v8引擎是作为解析JavaScript程序来运行的 nodejs是一个高性能的,第一个体现在他的JavaScript解析速度很快,v8引擎性能很高,第二个事件驱动和非阻塞 2.全局对 ...

  8. Node.js Learning Notes

    简介 简单的说 Node.js 就是运行在服务端的 JavaScript. Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台. Node.js是一个事件驱动I/O服务 ...

  9. Node.js NPM Package.json

    章节 Node.js NPM 介绍 Node.js NPM 作用 Node.js NPM 包(Package) Node.js NPM 管理包 Node.js NPM Package.json Nod ...

  10. 一目了然的 Node.js Windows10 安装篇

    本篇文章 介绍 NodeJS 的安装 及环境变量配置 Node JS 的 了解 1.Node.js简介 简单的说 Node.js 就是运行在服务端的 JavaScript.Node.js 是一个基于 ...

随机推荐

  1. python_子网划分计算器

    import ipaddress class Compute: def __init__(self, network): self.net = network # 功能1: ip子网划分 def ip ...

  2. 如果同时存在application.properties 和application.yml 文件的话,springboot会优先使用application.properties吗

    如果同时存在application.properties 和application.yml 文件的话,springboot会优先使用application.properties吗 总结:如果appli ...

  3. openframeworks 设置不显示控制台窗口

    1.设置Linker 下的System->选择Windows 2.设置入口函数,然后编译就可以了 int APIENTRY wWinMain(_In_ HINSTANCE hInstance, ...

  4. [iOS] 随手记录 IDFA 的一些相关内容

    IDFA  IDFA,广告标识符,类似于这一台设备的唯一性标识符,一般提供给第三方去做一些广告的关联. 但如果用户完全 重置系统,或者 "还原位置与隐私" 这个广告标示符会重新生成 ...

  5. git+jenkins+ansible+gitlab部署网站

  6. mysql查询数据是否连续增长

    记录一次比较查询,需求是比较内容是否一次比一次高,用来作为标签依据 大致问题如下 简化: 班级中有若干人,若干次考试.需要查询某人在考试时成绩越来越好(分数是每次都有增长) 思路: 1.使用group ...

  7. python常用的六个字符串处理方法

    1.upper(将小写字母转换为大写) 语法: str.upper() 参数: NA 实例: a = 'abc' b = a.upper() print(b) 结果: ABC 2.lower(将小写字 ...

  8. 实验5:开源控制器实践POX

    一.基础实验 建立拓扑: sudo mn --topo=single,3 --mac --controller=remote,ip=127.0.0.1,port=6633 --switch ovsk, ...

  9. Pytorch实战学习(八):基础RNN

    <PyTorch深度学习实践>完结合集_哔哩哔哩_bilibili Basic RNN ①用于处理序列数据:时间序列.文本.语音..... ②循环过程中权重共享机制 一.RNN原理 ① X ...

  10. 掌控安全学院SQL注入靶场-布尔盲注(二)

    首页打开如下 判断注入 闭合报错 先判断数据库的长度....