PublishFile.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <template>
  2. <el-row class="movie-list">
  3. <el-col :md="12" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  4. <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  5. <el-card class="box-card">
  6. <div slot="header" class="clearfix">
  7. <span>上传文件</span>
  8. </div>
  9. <div class="text item">
  10. <el-tooltip class="item" effect="dark" content="点击上传文件" placement="top-end">
  11. <el-upload
  12. class=""
  13. action=""
  14. :show-file-list="false"
  15. :http-request="fnUploadRequest"
  16. :on-success="handleAvatarSuccess"
  17. :before-upload="beforeAvatarUpload"
  18. >
  19. <img v-if="imageUrl" :src="imageUrl" class="avatar" alt="">
  20. <i v-else class="el-icon-plus avatar-uploader-icon" />
  21. </el-upload>
  22. </el-tooltip>
  23. </div>
  24. </el-card>
  25. </el-row>
  26. </el-col>
  27. <el-col :md="12" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  28. <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  29. <el-card class="box-card">
  30. <div slot="header" class="clearfix">
  31. <span>OSS 地址</span>
  32. </div>
  33. <div class="text item">
  34. <span>{{ imageUrl }}</span>
  35. <!-- <el-form ref="form" :model="imageUrl" label-width="80px">
  36. <el-form-item label="文件地址">
  37. <el-input v-model="imageUrl" style="padding-right: 1px" readonly />
  38. </el-form-item>
  39. </el-form>-->
  40. </div>
  41. </el-card>
  42. </el-row>
  43. </el-col>
  44. </el-row>
  45. </template>
  46. <script>
  47. import OSS from 'ali-oss'
  48. import { getSignedUrl, getStsToken } from '@/api/file'
  49. export default {
  50. name: 'PublishFile',
  51. data() {
  52. return {
  53. imageUrl: '',
  54. ossUrl: ''
  55. }
  56. },
  57. created() {
  58. },
  59. methods: {
  60. // 图片上传成功回调
  61. handleAvatarSuccess(res) {
  62. if (res) {
  63. const objectName = res.url.replace(this.ossUrl, '')
  64. const payload = {}
  65. payload.objectName = objectName
  66. getSignedUrl(payload).then(resp => {
  67. if (resp.code === 0) {
  68. this.imageUrl = resp.data
  69. // this.imageUrl = res.url
  70. }
  71. })
  72. }
  73. },
  74. beforeAvatarUpload(file) {
  75. const isJPG = file.type === 'image/jpeg'
  76. const isLt2M = file.size / 1024 / 1024 < 2
  77. if (!isJPG) {
  78. this.$message.error('上传头像图片只能是 JPG 格式!')
  79. }
  80. if (!isLt2M) {
  81. this.$message.error('上传头像图片大小不能超过 2MB!')
  82. }
  83. return isJPG && isLt2M
  84. },
  85. async fnUploadRequest(options) {
  86. try {
  87. const file = options.file // 拿到 file
  88. const index = file.name.lastIndexOf('.')
  89. const suffix = file.name.substr(index)
  90. getStsToken().then(resp => {
  91. if (resp.code === 0) {
  92. const credentials = resp.data
  93. this.ossUrl = credentials.ossUrl
  94. const client = new OSS({
  95. region: credentials.region,
  96. bucket: credentials.bucket,
  97. accessKeyId: credentials.accessKeyId,
  98. accessKeySecret: credentials.accessKeySecret,
  99. stsToken: credentials.securityToken
  100. })
  101. const objectId = credentials.objectId
  102. const objectName = 'image/i/' + objectId + suffix
  103. client.put(objectName, file).then(resp1 => {
  104. if (resp1.res.statusCode === 200) {
  105. console.log(resp1)
  106. options.onSuccess(resp1)
  107. } else {
  108. console.log(resp1)
  109. options.onError('上传失败')
  110. }
  111. })
  112. }
  113. })
  114. } catch (e) {
  115. options.onError('上传失败')
  116. }
  117. }
  118. }
  119. }
  120. </script>
  121. <style>
  122. .uploader-example {
  123. width: 500px;
  124. padding: 15px;
  125. margin: 40px auto 0;
  126. font-size: 12px;
  127. box-shadow: 0 0 10px rgba(0, 0, 0, .4);
  128. }
  129. .uploader-example .uploader-btn {
  130. margin-right: 4px;
  131. }
  132. .uploader-example .uploader-list {
  133. max-height: 440px;
  134. overflow: auto;
  135. overflow-x: hidden;
  136. overflow-y: auto;
  137. }
  138. .avatar-uploader .el-upload {
  139. border: 1px dashed #d9d9d9;
  140. border-radius: 6px;
  141. cursor: pointer;
  142. position: relative;
  143. overflow: hidden;
  144. }
  145. .avatar-uploader .el-upload:hover {
  146. border-color: #409EFF;
  147. }
  148. .avatar-uploader-icon {
  149. font-size: 28px;
  150. color: #8c939d;
  151. width: 320px;
  152. height: 240px;
  153. line-height: 178px;
  154. text-align: center;
  155. }
  156. .avatar {
  157. width: 320px;
  158. height: 240px;
  159. display: block;
  160. }
  161. </style>