前端直传文件到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=" ...
随机推荐
- iNeuOS工业互联平台,WEB组态(iNeuView)集成rtmp和websocket视频元件,支持海康、大华等摄像头实时显示视频
目 录 1. 概述... 1 2. 平台演示... 2 3. 硬件摄像头... 2 4. 视频流协议转换管理... 2 5. 组态视频元件 ...
- Excel 提取年月日①
问题场景 从任务数据表中比较所有任务的预计完成时间和实际完成时间,来判断该任务是逾期还是按期完成了,根据实际场景是不需要考虑时分秒,只需对比该任务预计完成和实际完成的年月日. 可通过提取年月日用函数进 ...
- PythonCrashCourse 第四章习题
Python 从入门到实践第四章习题 4.1想出至少三种你喜欢的比萨,将其名称存储在一个列表中,再使用for 循环将每种比萨的名称都打印出来 修改这个for 循环,使其打印包含比萨名称的句子,而不仅仅 ...
- mysql join update
SELECT CONCAT('UPDATE free_for_demo_orders SET product_id=',product_id,',order_created_time=','&quo ...
- selenium定位方法实例
selenium定位方法实例 首先打开浏览器输入微博的网址,将网页最大化,等待3秒 from selenium import webdriver import time driver = webdri ...
- CSS动画实例:圆的涟漪扩散
设页面中有<div class="circle "></div>,定义.circle的样式规则绘制一个半径为75px,边框厚度为4px的圆,再定义关键帧,使 ...
- soso官方:搜索引擎的对检索结果常用的评测方法
http://www.wocaoseo.com/thread-188-1-1.html 很久很久以前,搜索引擎还不象今天的百花齐放,人们对它的要求较低,只要它能把互连网上相关的网站搜出来, ...
- 焦大:seo思维进化论(下)
http://www.wocaoseo.com/thread-50-1-1.html 很多东西在不同地方其所有的价值和意义是不一样的,seo亦是如此.在seo操作中我觉得最核心的就是检索价值观和用户需 ...
- SICP之应用序和正则序
以一个题目来说明 (define (square x) (* x x)) (define (sum-squares x y) (+ (square x) (square y))) (define (f ...
- spring如何创建RESTful Service
REST REST,是指REpresentational State Transfer,有个精辟的解释什么是RESTful, 看url就知道要什么 看method就知道干什么 看status code ...