UploadFile.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <template>
  2. <el-row class="movie-list">
  3. <el-col :md="8" 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" :body-style="{ paddingTop: '0px' }">
  6. <div slot="header" class="clearfix">
  7. <span>选择目录</span>
  8. </div>
  9. <div class="item">
  10. <el-tree
  11. :accordion="true"
  12. :data="treeNode"
  13. :props="defaultProps"
  14. highlight-current
  15. @node-click="handleNodeClick"
  16. >
  17. <span slot-scope="{ node, data }">
  18. <span :class="data.icon">{{ node.label }}</span>
  19. </span>
  20. </el-tree>
  21. </div>
  22. </el-card>
  23. </el-row>
  24. </el-col>
  25. <el-col :md="16" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  26. <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  27. <el-card class="box-card">
  28. <div slot="header" class="clearfix">
  29. <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  30. <span>存储节点: <span style="color: blue">{{ ossUrl }}</span></span>
  31. </el-row>
  32. <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  33. <span>上传文件到目录: <span style="color: red">{{ currentDir }}</span></span>
  34. </el-row>
  35. </div>
  36. <div class="text item">
  37. <uploader
  38. class="uploader-example"
  39. :options="options"
  40. :auto-start="true"
  41. @file-added="onFileAdded"
  42. @file-success="onFileSuccess"
  43. @file-progress="onFileProgress"
  44. @file-error="onFileError"
  45. >
  46. <uploader-unsupport />
  47. <uploader-drop>
  48. <p>拖动文件到此处或</p>
  49. <uploader-btn :attrs="attrs">选择文件</uploader-btn>
  50. </uploader-drop>
  51. <uploader-list />
  52. </uploader>
  53. </div>
  54. </el-card>
  55. </el-row>
  56. </el-col>
  57. </el-row>
  58. </template>
  59. <script>
  60. import { getServerInfo } from '@/api/content'
  61. import { addDiskFile, getFolderTree } from '@/api/disk'
  62. export default {
  63. name: 'UploadFile',
  64. data() {
  65. return {
  66. // ****************************************************************************************************************
  67. options: {
  68. target: process.env.VUE_APP_OSS_URL,
  69. chunkSize: 1024 * 1024 * 1024 * 10, // 10GiB
  70. fileParameterName: 'file',
  71. testChunks: false,
  72. query: (file, chunk) => {
  73. return {
  74. channelId: 1
  75. }
  76. },
  77. headers: {
  78. Authorization: ''
  79. },
  80. withCredentials: true
  81. },
  82. attrs: {
  83. accept: '*'
  84. },
  85. // ****************************************************************************************************************
  86. ossUrl: null,
  87. // ****************************************************************************************************************
  88. treeNode: [],
  89. defaultProps: {
  90. children: 'children',
  91. label: 'label',
  92. value: 'value'
  93. },
  94. currentDir: '/',
  95. // ****************************************************************************************************************
  96. pid: 0,
  97. form: {
  98. fileUrl: null,
  99. imageUrl: null
  100. }
  101. }
  102. },
  103. created() {
  104. document.title = '上传文件'
  105. getServerInfo(1).then(resp => {
  106. if (resp.code === 0) {
  107. const resData = resp.data
  108. this.ossUrl = resData.ossUrl
  109. this.options.target = resData.ossUrl
  110. this.options.chunkSize = resData.maxSize
  111. this.options.headers.Authorization = 'Bearer ' + resData.token
  112. } else {
  113. this.$notify({
  114. title: '提示',
  115. message: '获取 OSS 服务器地址失败, 暂时无法上传文件',
  116. type: 'error',
  117. duration: 3000
  118. })
  119. }
  120. }).catch(error => {
  121. this.$notify({
  122. title: '提示',
  123. message: error.message,
  124. type: 'warning',
  125. duration: 3000
  126. })
  127. })
  128. getFolderTree().then(resp => {
  129. if (resp.code === 0) {
  130. this.treeNode = resp.data
  131. } else {
  132. this.$notify({
  133. title: '提示',
  134. message: resp.msg,
  135. type: 'error',
  136. duration: 3000
  137. })
  138. }
  139. }).catch(error => {
  140. this.$notify({
  141. title: '提示',
  142. message: error.message,
  143. type: 'warning',
  144. duration: 3000
  145. })
  146. })
  147. },
  148. mounted() {
  149. },
  150. methods: {
  151. // ****************************************************************************************************************
  152. onFileAdded(file) {
  153. if (file.file.size > 1024 * 1024 * 1024 * 5) {
  154. file.cancel()
  155. this.$notify({
  156. title: '提示',
  157. message: '视频文件应小于 5GiB',
  158. type: 'warning',
  159. duration: 3000
  160. })
  161. }
  162. },
  163. onFileProgress(rootFile, file, chunk) {
  164. },
  165. onFileSuccess(rootFile, file, response, chunk) {
  166. const resp = JSON.parse(response)
  167. if (resp.code === 0) {
  168. this.form.fileUrl = resp.data.url
  169. const jsonData = {}
  170. jsonData.ossUrl = this.ossUrl
  171. jsonData.uploadId = resp.data.uploadId
  172. jsonData.pid = this.pid
  173. this.addDiskFile(jsonData)
  174. } else {
  175. this.$notify({
  176. title: '提示',
  177. message: resp.msg,
  178. type: 'warning',
  179. duration: 3000
  180. })
  181. }
  182. },
  183. onFileError(rootFile, file, response, chunk) {
  184. const resp = JSON.parse(response)
  185. this.$notify({
  186. message: resp.msg,
  187. type: 'warning',
  188. duration: 3000
  189. })
  190. },
  191. // ****************************************************************************************************************
  192. handleNodeClick(data) {
  193. this.pid = data.fileId
  194. this.currentDir = data.label
  195. },
  196. addDiskFile(jsonData) {
  197. addDiskFile(jsonData).then(resp => {
  198. if (resp.code !== 0) {
  199. this.$notify({
  200. message: resp.msg,
  201. type: 'warning',
  202. duration: 3000
  203. })
  204. }
  205. })
  206. }
  207. }
  208. }
  209. </script>
  210. <style>
  211. .uploader-example {
  212. width: 500px;
  213. padding: 15px;
  214. margin: 40px auto 0;
  215. font-size: 12px;
  216. box-shadow: 0 0 10px rgba(0, 0, 0, .4);
  217. }
  218. .uploader-example .uploader-btn {
  219. margin-right: 4px;
  220. }
  221. .uploader-example .uploader-list {
  222. max-height: 440px;
  223. overflow: auto;
  224. overflow-x: hidden;
  225. overflow-y: auto;
  226. }
  227. .avatar-uploader .el-upload {
  228. border: 1px dashed #d9d9d9;
  229. border-radius: 6px;
  230. cursor: pointer;
  231. position: relative;
  232. overflow: hidden;
  233. }
  234. .avatar-uploader .el-upload:hover {
  235. border-color: #409EFF;
  236. }
  237. .avatar-uploader-icon {
  238. font-size: 28px;
  239. color: #8c939d;
  240. width: 320px;
  241. height: 240px;
  242. line-height: 178px;
  243. text-align: center;
  244. }
  245. .avatar {
  246. width: 320px;
  247. height: 240px;
  248. display: block;
  249. }
  250. </style>