前端直传文件到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=" ...
随机推荐
- 快速搭建一个Vue-cli项目(简单到爆炸)
引言: 2013-2020年,这7年是web前端技术的一个高速发展期,也是前端开发岗位的考验期. 在我接触前端时,JQuery十分热门,开发者从原生JS到JQ的应用,可以说是大大提高了开发效率,也被广 ...
- 反制面试官 | 14张原理图 | 再也不怕被问 volatile!
反制面试官 | 14张原理图 | 再也不怕被问 volatile! 悟空 爱学习的程序猿,自主开发了Java学习平台.PMP刷题小程序.目前主修Java.多线程.SpringBoot.SpringCl ...
- JDK1.8源码学习-Object
JDK1.8源码学习-Object 目录 一.方法简介 1.一个本地方法,主要作用是将本地方法注册到虚拟机中. private static native void registerNatives() ...
- kvm 虚拟机中鼠标不同步的问题解决方法
在<devices>标签下添加 <input type='tablet' bus='usb'/>
- go二叉树、struct、接口
二叉树实现.及遍历 二叉树定义 type Student struct { Name string left* Student right* Student } 如果每个节点有两个指针分别用来指向左子 ...
- Hadoop 3.x 与Hadoop 2.x 的区别和优化点
Hadoop 3.x 与Hadoop 2.x 的区别和优化点 通用性 1.精简Hadoop内核,包括剔除过期的API和实现,将默认组件实现替换成最高效的实现(比如将FileOutputCommitte ...
- C# 接口的Get、Post、WebService请求方法一览,值得收藏
C# 接口的Get.Post.WebService请求方法一览,值得收藏 public static class HttpHelper { const string DEFAULT_USER_AGEN ...
- 【小白学AI】GBDT梯度提升详解
文章来自微信公众号:[机器学习炼丹术] 文章目录: 目录 0 前言 1 基本概念 2 梯度 or 残差 ? 3 残差过于敏感 4 两个基模型的问题 0 前言 先缕一缕几个关系: GBDT是gradie ...
- new Map()详细介绍与对比
说明: Map结构提供了“值—值”的对应,是一种更完善的Hash结构实现.如果你需要“键值对”的数据结构,Map比Object更合适.它类似于对象,也是键值对的集合,但是“键”的范围不限于字符串, ...
- Android开发之常用必备工具类图片bitmap转成字符串string与String字符串转换为bitmap图片格式
作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 QQ986945193 博客园主页:http://www.cnblogs.com/mcxiaobing ...