引入阿里云oss的js
<script src="https://gosspublic.alicdn.com/aliyun-oss-sdk-4.4.4.min.js"></script>
<input type="file" name="file" @change='selectFile' multiple="multiple"/>上传图片/文件
mounted () {
this.initConfig() // 调用后台接口获取阿里云上传下载通行证
}
methods: {
initConfig () {
// 初始化oss权限
let url = 'document.getAccess'
let params = {
type: 'H'
}
this.$api.send(url, params).then((response) => {
if (response.status === 200) {
let data = response.body.data.data
/* global OSS */ // 去掉esllint对OSS的校验
this.client = new OSS.Wrapper({
region: 'oss-cn-shenzhen',
accessKeyId: 'your accessKeyId',
accessKeySecret: 'your accessKeySecret',
stsToken: 'your stsToken',
bucket: 'xx'
})
}
})
},
selectFile (e) {
// 选择文件
for (let i = 0; i < e.target.files.length; i++) {
this.pushFile(e.target.files[i])
}
},
pushFile (file) {
let that = this
let _file = file
var storeAs = '' // 传到oss上的名字
// 调用上传方法
that.client.multipartUpload('cloudStorage/' + storeAs, _file, {
progress: function* (percentage) {
let fileloadingNum = Math.ceil(percentage * 100) + '%'
console.log(fileloadingNum) // 上传文件进度
}
}).then(function (result) {
// 调用后台添加文件的接口
let url = 'netdisc.addDoc'
let params = {
data: 'xx'
}
that.$api.send(url, params).then((response) => {
if (response.status === 200) {
// 上传成功
}
})
}).catch(function (err) {
// 上传失败,弹出上传失败的消息
})
}
}
如果传到阿里云的图片要展示出来,要在src的图片路径后面加上阿里云后缀,这样用苹果手机拍的照片就不会出现图片翻转的问题,像这样
xxx.JPG?x-oss-process=image/auto-orient,1/resize,m_fill,w_1600
如果图片要用canvas做压缩, 得到的是base64数据,要转换成blob对象,再转为buffer流。用put上传
有些手机不支持canvas直接转为blob对象可以引入canvas-to-blob.min.js 将canvas转为blob对象
blob插件地址: https://github.com/blueimp/Ja…
获得图片的方向,引入exif.js
exif.js 官网地址 http://code.ciaoca.com/javasc…
项目中都是用<script>标签直接在index.html中引用的
pushFile (file) {
let that = this
if (['jpeg', 'png', 'jpg'].indexOf(file.type.split('/')[1]) < 0) {
alert('只支持jpg/png格式的图片')
return false
}
// orient=>照片的角度
/* global EXIF */
let orient
EXIF.getData(file, function () {
orient = EXIF.getTag(this, 'Orientation')
})
// 压缩图片需要的一些元素和对象
let reader = new FileReader()
let img = new Image()
// 选择得是图片
if (file.type.indexOf('image') === 0) {
reader.readAsDataURL(file)
}
// 缩放图片需要的canvas
let canvas = document.createElement('canvas')
let context = canvas.getContext('2d')
// base64 地址加载完后
img.onload = function () {
// 图片原始尺寸
let originWidth = this.width
let oringinHeight = this.height
// 最大尺寸限制
let maxWidth = 800
let maxHeight = 800
// 目标尺寸
let targetWidth = originWidth
let targetHeight = oringinHeight
// 图片尺寸超过800x800的限制
if (originWidth > maxWidth || oringinHeight > maxHeight) {
if (originWidth / oringinHeight > maxWidth / maxHeight) {
// 更宽
targetWidth = maxWidth
targetHeight = Math.round(maxWidth * (oringinHeight / originWidth))
} else {
targetHeight = maxHeight
targetWidth = Math.round(maxHeight * (originWidth / oringinHeight))
}
}
// canvas 对图片进行缩放
canvas.width = targetWidth
canvas.height = targetHeight
// 清除画布
context.clearRect(0, 0, targetWidth, targetHeight)
// 图片压缩
context.drawImage(img, 0, 0, targetWidth, targetHeight)
if (orient !== '' && orient !== 1) {
// orient === 1是正常的
switch (orient) {
case 6: // 需要顺时针向左90度旋转
that.rotateImg(img, 'left', canvas, targetWidth, targetHeight)
break
case 8: // 需要逆时针向右90度旋转
that.rotateImg(img, 'right', canvas, targetWidth, targetHeight)
break
case 3: // 需要180度旋转
that.rotateImg(img, 'right', canvas, targetWidth, targetHeight)
that.rotateImg(img, 'right', canvas, targetWidth, targetHeight)
break
}
}
if (canvas.toBlob) {
canvas.toBlob(function (blob) {
// 在这里实现上传操作
let reader2 = new FileReader()
reader2.readAsArrayBuffer(blob)
reader2.onload = function (event) {
let buffer = new OSS.Buffer(event.target.result)
that.client.put(storeAs, buffer).then((result) => {
if (result.url) {
// 获得图片地址
that.src= result.url
}
}).catch((err) => {
console.log(err)
alert('上传失败, 请重新上传')
})
}
}, file.type || 'image/png')
}
}
rotateImg (img, direction, canvas, targetWidth, targetHeight) {
// 最小与最大旋转方向,图片旋转4次后回到原方向
var minstep = 0
var maxstep = 3
if (img === null) return
// img的高度和宽度不能在img元素隐藏后获取,否则会出错
var step = 2
if (step === null) {
step = minstep
}
if (direction === 'right') {
step++
// 旋转到原位置,即超过最大值
step > maxstep && (step = minstep)
} else {
step--
step < minstep && (step = maxstep)
}
// 旋转角度以弧度值为参数
let degree = step * 90 * Math.PI / 180
var ctx = canvas.getContext('2d')
switch (step) {
case 0:
canvas.width = targetWidth
canvas.height = targetHeight
ctx.clearRect(0, 0, targetWidth, targetHeight)
ctx.drawImage(img, 0, 0, targetWidth, targetHeight)
break
case 1:
canvas.width = targetHeight
canvas.height = targetWidth
ctx.rotate(degree)
ctx.clearRect(0, 0, targetHeight, targetWidth)
ctx.drawImage(img, 0, -targetHeight, targetWidth, targetHeight)
break
case 2:
canvas.width = targetWidth
canvas.height = targetHeight
ctx.rotate(degree)
ctx.clearRect(0, 0, targetWidth, targetHeight)
ctx.drawImage(img, -targetWidth, -targetHeight, targetWidth, targetHeight)
break
case 3:
canvas.width = targetHeight
canvas.height = targetWidth
ctx.rotate(degree)
ctx.clearRect(0, 0, targetHeight, targetWidth)
ctx.drawImage(img, -targetHeight, 0, targetWidth, targetHeight)
break
}
}
}