前端直传文件到aliyun OSS
<template>
<div id="container">
<div class="img-item m-1 upload-file" id="selectfiles" @click="axioSignal">
<span class="mask"></span>
<b-form-file multiple accept="image/*"></b-form-file>
</div>
</div>
</template>
<script>
import plupload from 'plupload'
export default {
name: 'formImg',
data () {
return {
signal: {
policyBase64: '',
accessid: '',
signature: ''
},
imgs: [],
imgUrl: [],
uploader: {}
}
},
mounted () {
let _this = this
this.uploader = new plupload.Uploader({
runtimes: 'html5,flash,silverlight,html4',
browse_button: 'selectfiles',
container: document.getElementById('container'),
init: {
PostInit: function () {
console.log(`PostInit`)
// _this.axioSignal()
// console.log(_this.onImgs)
// _this.imgs.push(_this.onImgs)
// _this.imgs = _this.onImgs
// console.log(_this.onImgs)
// console.log(_this.imgs)
},
FilesAdded: function (up, files) {
_this.uploader.start()
// plupload.each(files, function (file) {
console.log(`FilesAdded`)
// })
},
BeforeUpload: function (up, file) {
_this.set_upload_param(up, file.name, true)
},
// UploadProgress: function (up, file) {
// console.log(`UploadProgress`)
// },
FileUploaded: function (up, file, info) {
console.log(`FileUploaded`)
if (info.status === 200) {
_this.imgUrl.forEach(item => {
_this.imgs.push(item)
_this.imgUrl = []
})
_this.$emit('imgs', _this.imgs)
}
}
// Error: function (up, err) {
// document.getElementById('console').appendChild(document.createTextNode('\nError xml:' + err.response))
// }
}
})
// _this.uploader.addFileFilter('max_file_size', function (maxSize, file, cb) {
// var undef
//
// maxSize = plupload.parseSize(maxSize)
//
// // Invalid file size
// if (file.size !== undef && maxSize && file.size > maxSize) {
// this.trigger('Error', {
// code: plupload.FILE_SIZE_ERROR,
// message: plupload.translate('File size error.'),
// file: file
// })
// cb(false)
// } else {
// cb(true)
// }
// })
_this.uploader.init()
},
methods: {
axioSignal () {
console.log(`axioSignal`)
this.$axios({
method: 'get',
url: 'http://www.lyly360.com/fs/ali/signal'
}).then(res => {
this.signal.policyBase64 = res.policy
this.signal.accessid = res.accessid
this.signal.signature = res.signature
this.signal.host = res.host
})
},
formatNames (files) {
console.log(`formatNames`)
if (files.length === 1) {
return files[0].name
} else {
return `${files.length} files selected`
}
},
// postfiles () {
// this.uploader.start()
// },
random_string (len) {
len = len || 32
var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
var maxPos = chars.length
var pwd = ''
for (var i = 0; i < len; i++) {
pwd += chars.charAt(Math.floor(Math.random() * maxPos))
}
return pwd
},
get_suffix (filename) {
var pos = filename.lastIndexOf('.')
var suffix = ''
if (pos !== -1) {
suffix = filename.substring(pos)
}
return suffix
},
calculate_object_name (filename) {
var suffix = this.get_suffix(filename)
var randName = this.random_string(10) + suffix
return randName
},
set_upload_param (up, filename) {
var randFileName = 'file' + this.calculate_object_name(filename)
this.imgUrl.push(this.signal.host + '/' + randFileName + '-ly.jpg')
var newMparams = {
'key': randFileName,
'policy': this.signal.policyBase64,
'OSSAccessKeyId': this.signal.accessid,
'success_action_status': '200',
'signature': this.signal.signature
}
up.setOption({
'url': this.signal.host,
'multipart_params': newMparams
})
}
}
}
</script>
<style lang="stylus" scoped>
.upload-file {
height: 100px;
width: 100px;
vertical-align: middle;
text-align: center;
line-height: 100px;
}
.upload-file .custom-file {
height: 100px;
width: 100px;
opacity: 0;
}
>>>.upload-file .custom-file input {
height: 80%;
width: 80%;
overflow hidden
}
.mask {
position: absolute;
width: 100%;
height: 100%;
background-image: url(../assets/upload.png);
background-size: 100px 100px;
background-position: center center
left: 0;
top: 0;
}
</style>
前端直传文件到aliyun OSS的更多相关文章
- Aliyun Oss 上传文件
目录 Aliyun OSS OSS 简介 OSS 基本概念 OSS 功能概述 OSS 使用 创建存储空间Bucket 创建子目录 Java编码 测试 Aliyun OSS OSS 简介 阿里云对象存储 ...
- 直传文件到Azure Storage的Blob服务中
(此文章同时发表在本人微信公众号“dotNET每日精华文章”,欢迎右边二维码来关注.) 题记:为了庆祝获得微信公众号赞赏功能,忙里抽闲分享一下最近工作的一点心得:如何直接从浏览器中上传文件到Azure ...
- Windows下编译使用Aliyun OSS PHP SDK
摘要: WIN环境下搭建Aliyun OSS PHP SDK编译运行环境.从PHP的安装逐步完成,SDK的编译运行.即使没有任何PHP基础,也能顺利完成. 安装环境:Win7 64 + PHP 5.6 ...
- aliyun oss操作汇总
// endpoint以杭州为例,其它region请按实际情况填写 String endpoint = "http://oss-cn-hangzhou.aliyuncs.com"; ...
- vue 上传图片到阿里云(前端直传:不推荐)
为何要这样做:减轻后端数据库压力(个人觉得于前端没啥用,谁返回来都行) 代码部分: <template> <div class="upLoad"> < ...
- HTML5 File API — 让前端操作文件变的可能
前言 在 HTML5 File API 出现之前,前端对于文件的操作是非常有局限性的,大多需要配合后端实现.出于安全角度考虑,从本地上传文件时,代码不可能获取文件在用户本地的地址,所以纯前端不可能完成 ...
- 前端js文件合并三种方式
最近在思考前端js文件该如何合并,当然不包括不能合并文件,而是我们能合并的文件,想了想应该也只有三种方式. 三个方式如下: 1. 一个大文件,所有js合并成一个大文件,所有页面都引用它. 2. 各个页 ...
- Aliyun OSS SDK 异步分块上传导致应用异常退出
问题描述: 使用Aliyun OSS SDK的BeginUploadPart/EndUploadPart执行异步分块上传操作,程序出现错误并异常退出! 原因分析: Using .NET Framewo ...
- js前端读写文件的方法(json、excel)
1.前端读取文件的实现 关键:利用文件上传对话框预览本地文件.利用FileReader读取文件 前端预览本地文件 <input tabindex="-1" id=" ...
随机推荐
- 免费验证码接收网站&不停开小号方法
手机号注册:免费验证码接收网站 0.http://getfreesmsnumber.com/ 9个外国网站,访问后要快点点击链接,否则2秒会检测出adblocker 1.http://smsrecei ...
- C++ Templates (1.2 模板实参推断 Template Argument Deduction)
返回完整目录 目录 1.2 模板实参推断 Template Argument Deduction 1.2 模板实参推断 Template Argument Deduction 当调用函数模板(如max ...
- 操作系统-进程(3)Linux下的进程相关命令
操作系统给予这个内存中的单元一个标识符(PID)依据登入者的UID/GID(/etc/passwd) 衍生出的其它程序(子程序),一般情况也,也会沿用这个程序(父程序)的相关权限 ParentID(P ...
- Rethinking the performance comparison between SNNS and ANNS
郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! Abstract ANN是通向AI的一种流行方法,它已经通过成熟的模型,各种基准,开源数据集和强大的计算平台获得了非凡的成功.SNN是一类 ...
- SNN对抗攻击笔记
SNN对抗攻击笔记: 1. 解决SNN对抗攻击中脉冲与梯度数据格式不兼容性以及梯度消失问题: G2S Converter.Gradient Trigger[1] 2. 基于梯度的对抗攻击方式: FGS ...
- Windows servers 2008 环境下,域控DC和DNS,分离搭建过程。
近来做有关于window服务器方面运维的实验,正好借此记录下来,便于日后回顾. 通常情况下,域控DC服务器和DNS服务器一般不在一起,所以需要将其分开建立.而这个时候两个服务器的建立有先后顺序,本文会 ...
- springsession
Spring Session 一. HttpSession 回顾 1 什么是 HttpSession 是 JavaWeb 服务端提供的用来建立与客户端会话状态的对象. 二. Session 共享 1 ...
- 什么是URL标准化
http://www.wocaoseo.com/thread-194-1-1.html RL标准化是来自于国外matt cutts的博客文章,期望能给蜘蛛一个规范化的URL作为收录的标准.举个简单的例 ...
- 手写@Service、@Autowired、@Transactional注解,实现spring ioc和spring事务
自定义@Service注解 @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface Custom ...
- 力扣Leetcode 200. 岛屿数量
岛屿数量 给你一个由 '1'(陆地)和 '0'(水)组成的的二维网格,请你计算网格中岛屿的数量. 岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成. 此外,你可以假设该网 ...