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. qt creator 常量中有换行符(转)

    这篇文章写于QT5.6的诞生,它是QT5的第一个长期支持版.这也是QT5已成熟的里程碑. 我搭建的环境是:Win10+VS2015+QT5.6+QtCreator 在安装VS2015的时候,若是用Qt ...

  2. Linux 第六节( 磁盘系统,挂载,分区,格式化)

    /dev/st0   磁带机 /dev/lp     打印机 /dev/cdrom  光驱 /dev/sd       scsi接口硬盘 sata接口硬盘  U盘(sda,sdb,sdc   分别对应 ...

  3. vscode1.50配置python虚拟环境

    1.首先你需要   创建好虚拟环境,如果不会 可以先点击学习一下 https://www.cnblogs.com/shyern/p/11284127.html      (创建虚拟环境的博客) 2.打 ...

  4. 前端之Vue day08 Vue3项目搭建、setup、toRefs

    一.Vue3 介绍 # 新项目使用vue3,有部分老项目使用vue2 # vue3 的变化 1.性能的提升 -打包大小减少41% -初次渲染快55%, 更新渲染快133% -内存减少54% 2.源码的 ...

  5. drop table后,约束会被删除吗?

    create table test1(id number); insert into test1 values(11);insert into test1 values(22);insert into ...

  6. UE4常用快捷键

    编辑器快捷键 按键 操作 W 选择"移动"工具 E 选择"旋转"工具 R 选择"缩放"工具 F 聚焦对象 End 落到地面 Alt + En ...

  7. nginx转发端口路由器再转发

    场景 nginx 转发端口 路由器二次转发了,端口不一样 (shiro 或者其他一些权限控制架构会自动跳转,导致的端口不对.) proxy_set_header Host $host:$proxy_p ...

  8. Tunnel

    Tunnel既不是给https用的,也不是给代理用的,是给https代理用的 之所以以前老觉得Https也有一个tunnel,是因为每次看https请求,fiddler本身就是http代理,本来就会有 ...

  9. .Net Core WebApi 控制器自动创建文件夹上传图片

    /// <summary> /// 异步图片或文件上传 /// </summary> /// <param name="formFile">&l ...

  10. 【PyCharm】PyCharm设置深色背景

    操作步骤 1.依次点击File->Settings->Appearance&Behavior->Appearance 2.选择Theme为Darcula