| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <template>
- <el-row class="movie-list">
- <el-col :md="12" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
- <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
- <el-card class="box-card">
- <div slot="header" class="clearfix">
- <span>上传文件</span>
- </div>
- <div class="text item">
- <el-tooltip class="item" effect="dark" content="点击上传文件" placement="top-end">
- <el-upload
- class=""
- action=""
- :show-file-list="false"
- :http-request="fnUploadRequest"
- :on-success="handleAvatarSuccess"
- :before-upload="beforeAvatarUpload"
- >
- <img v-if="imageUrl" :src="imageUrl" class="avatar" alt="">
- <i v-else class="el-icon-plus avatar-uploader-icon" />
- </el-upload>
- </el-tooltip>
- </div>
- </el-card>
- </el-row>
- </el-col>
- <el-col :md="12" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
- <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
- <el-card class="box-card">
- <div slot="header" class="clearfix">
- <span>OSS 地址</span>
- </div>
- <div class="text item">
- <span>{{ imageUrl }}</span>
- <!-- <el-form ref="form" :model="imageUrl" label-width="80px">
- <el-form-item label="文件地址">
- <el-input v-model="imageUrl" style="padding-right: 1px" readonly />
- </el-form-item>
- </el-form>-->
- </div>
- </el-card>
- </el-row>
- </el-col>
- </el-row>
- </template>
- <script>
- import OSS from 'ali-oss'
- import { getSignedUrl, getStsToken } from '@/api/file'
- export default {
- name: 'PublishFile',
- data() {
- return {
- imageUrl: '',
- ossUrl: ''
- }
- },
- created() {
- },
- methods: {
- // 图片上传成功回调
- handleAvatarSuccess(res) {
- if (res) {
- const objectName = res.url.replace(this.ossUrl, '')
- const payload = {}
- payload.objectName = objectName
- getSignedUrl(payload).then(resp => {
- if (resp.code === 0) {
- this.imageUrl = resp.data
- // this.imageUrl = res.url
- }
- })
- }
- },
- beforeAvatarUpload(file) {
- const isJPG = file.type === 'image/jpeg'
- const isLt2M = file.size / 1024 / 1024 < 2
- if (!isJPG) {
- this.$message.error('上传头像图片只能是 JPG 格式!')
- }
- if (!isLt2M) {
- this.$message.error('上传头像图片大小不能超过 2MB!')
- }
- return isJPG && isLt2M
- },
- async fnUploadRequest(options) {
- try {
- const file = options.file // 拿到 file
- const index = file.name.lastIndexOf('.')
- const suffix = file.name.substr(index)
- getStsToken().then(resp => {
- if (resp.code === 0) {
- const credentials = resp.data
- this.ossUrl = credentials.ossUrl
- const client = new OSS({
- region: credentials.region,
- bucket: credentials.bucket,
- accessKeyId: credentials.accessKeyId,
- accessKeySecret: credentials.accessKeySecret,
- stsToken: credentials.securityToken
- })
- const objectId = credentials.objectId
- const objectName = 'image/i/' + objectId + suffix
- client.put(objectName, file).then(resp1 => {
- if (resp1.res.statusCode === 200) {
- console.log(resp1)
- options.onSuccess(resp1)
- } else {
- console.log(resp1)
- options.onError('上传失败')
- }
- })
- }
- })
- } catch (e) {
- options.onError('上传失败')
- }
- }
- }
- }
- </script>
- <style>
- .uploader-example {
- width: 500px;
- padding: 15px;
- margin: 40px auto 0;
- font-size: 12px;
- box-shadow: 0 0 10px rgba(0, 0, 0, .4);
- }
- .uploader-example .uploader-btn {
- margin-right: 4px;
- }
- .uploader-example .uploader-list {
- max-height: 440px;
- overflow: auto;
- overflow-x: hidden;
- overflow-y: auto;
- }
- .avatar-uploader .el-upload {
- border: 1px dashed #d9d9d9;
- border-radius: 6px;
- cursor: pointer;
- position: relative;
- overflow: hidden;
- }
- .avatar-uploader .el-upload:hover {
- border-color: #409EFF;
- }
- .avatar-uploader-icon {
- font-size: 28px;
- color: #8c939d;
- width: 320px;
- height: 240px;
- line-height: 178px;
- text-align: center;
- }
- .avatar {
- width: 320px;
- height: 240px;
- display: block;
- }
- </style>
|