uploadFile.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <div class="admin-uploadAlCloud">
  3. <el-upload
  4. ref="upload"
  5. action="action"
  6. class="avatar-uploader"
  7. :http-request="uploadFun"
  8. :show-file-list="false">
  9. <img v-if="imageUrl" :src="imageUrl" class="avatar" alt="头像">
  10. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  11. </el-upload>
  12. <el-button v-if="showBtnFlag&&btnType==='delete'" type="primary" size="small" class="delete-btn" @click="btnClick(btnType)">删除</el-button>
  13. </div>
  14. </template>
  15. <script>
  16. import { getFilePolicy } from '@/api/AlCloud.js';
  17. // describe: 上传组件 author: Wgy date:2020-03-30
  18. /*
  19. * showBtnFlag:是否显现按钮
  20. * btnType:按钮类型 显示按钮 默认type为 'delete'
  21. *imageUrl:入参图片
  22. * 父页面 example:<upload-file :imageUrl="imageUrl1" @btnFun="btnFun" @getFileUrl="getFileFun"></upload-file>
  23. * */
  24. export default {
  25. name: "UploadAlCloud",
  26. props: {
  27. showBtnFlag: {
  28. type: Boolean,
  29. default: true
  30. },
  31. btnType: {
  32. type: String,
  33. default: 'delete',
  34. },
  35. imageUrl: {
  36. type: String,
  37. default: '',
  38. },
  39. },
  40. data() {
  41. return {
  42. afterUploadImg: '',
  43. };
  44. },
  45. methods: {
  46. //按钮
  47. btnClick(data){
  48. let response ={
  49. type:data,
  50. imageUrl:this.imageUrl
  51. }
  52. this.$emit('btnFun', response);
  53. },
  54. // 自定义上传
  55. uploadFun(params) {
  56. console.log(params);
  57. if (!params) {
  58. return;
  59. }
  60. const suffixList = params.file.name.split('.').pop();
  61. const options = {
  62. prefix: 'resource/portal/',
  63. suffix: suffixList,
  64. };
  65. const loading = this.$loading({background:"rgba(0, 0, 0, 0.7)"});
  66. getFilePolicy(options).then((res) => {
  67. if (res.code === 0) {
  68. // 二进制文件通过forData对象进行传递
  69. const FormDataForAl = new FormData();
  70. const multipartParams = Object.assign({}, res.data, {
  71. Filename: `images/${params.filename}`,
  72. success_action_status: '200',
  73. });
  74. // 参数数据
  75. FormDataForAl.append('key', multipartParams.key);
  76. FormDataForAl.append('policy', multipartParams.policy);
  77. FormDataForAl.append('signature', multipartParams.signature);
  78. FormDataForAl.append('OSSAccessKeyId', multipartParams.accessid);
  79. FormDataForAl.append('success_action_status', multipartParams.success_action_status);
  80. // OSS要求, file放到最后
  81. FormDataForAl.append('file', params.file);
  82. this.$axios.post(multipartParams.url, FormDataForAl).then(alRes => {
  83. if (alRes.status === 200) {
  84. this.afterUploadImg = `${multipartParams.url}/${multipartParams.key}`;
  85. this.$emit('getFileUrl', this.afterUploadImg);
  86. loading.close();
  87. }else {
  88. loading.close();
  89. }
  90. }).catch(e => {
  91. loading.close();
  92. });
  93. }else {
  94. loading.close();
  95. }
  96. });
  97. }
  98. },
  99. }
  100. </script>
  101. <style lang="scss">
  102. .admin-uploadAlCloud {
  103. display: block;
  104. .avatar-uploader .el-upload {
  105. background: #fff;
  106. border: 1px dashed #d9d9d9;
  107. border-radius: 6px;
  108. cursor: pointer;
  109. position: relative;
  110. overflow: hidden;
  111. }
  112. .avatar-uploader .el-upload:hover {
  113. border-color: #409EFF;
  114. }
  115. .avatar-uploader-icon {
  116. font-size: 28px;
  117. color: #8c939d;
  118. width: 178px;
  119. height: 178px;
  120. line-height: 178px;
  121. text-align: center;
  122. }
  123. .avatar {
  124. width: 178px;
  125. height: 178px;
  126. display: block;
  127. }
  128. @media screen and (max-width: 1440px){
  129. .avatar{
  130. width: 140px; // 必须指定宽高
  131. height: 140px;
  132. line-height: 140px;
  133. }
  134. .avatar-uploader-icon{
  135. width: 140px;
  136. height: 140px;
  137. line-height: 140px;
  138. }
  139. }
  140. }
  141. </style>