Test.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <template>
  2. <div>
  3. <el-upload
  4. :action="uploadUrl"
  5. :headers="headers"
  6. :file-list="urls"
  7. :limit="limit"
  8. :multiple="multiple"
  9. list-type="picture-card"
  10. :before-upload="beforeUnload"
  11. :on-exceed="exceed"
  12. :on-success="successUpload"
  13. :on-error="errorUpload">
  14. <i class="el-icon-plus"></i>
  15. <div slot="file" slot-scope="scope">
  16. <el-image
  17. :src="scope.file.url" alt=""
  18. @click="handlePictureCardPreview(scope)"
  19. :preview-src-list="srcList">
  20. </el-image>
  21. </div>
  22. </el-upload>
  23. </div>
  24. </template>
  25. <script>
  26. import lrz from 'lrz'
  27. export default {
  28. data() {
  29. return {
  30. limit: 10,
  31. multiple: true,
  32. uploadUrl: this.serverUrl + '/sys/user/upload',
  33. headers: {
  34. 'sessionId': this.storageUtil.read("sessionId")
  35. },
  36. urls: [{url: this.serverUrl +"/public/uploads/record1572577377866.png"},
  37. {url: this.serverUrl +"/public/uploads/record1572577377866.png"}],
  38. srcList: []
  39. };
  40. },
  41. methods: {
  42. handleRemove(file, fileList) {
  43. // console.log("删除", file, fileList);
  44. },
  45. handlePictureCardPreview(file) {
  46. // console.log("预览", file);
  47. },
  48. beforeUnload(file) {
  49. lrz(file).then(rst => {
  50. const isLt2M = rst.fileLen / 1024 / 1024 < 2 && rst.fileLen > 0;
  51. if (!isLt2M) {
  52. this.$message.error('上传图片太大,请重新选择!');
  53. return;
  54. }
  55. file = rst.file;
  56. // console.log('图',rst);
  57. }
  58. ).catch(error => {
  59. that.$message.error(error.message);
  60. }
  61. );
  62. // console.log("开始上传", file);
  63. },
  64. successUpload(response, file, fileList) {
  65. this.urls.push(response.data);
  66. console.log("successUpload", response, file, fileList);
  67. },
  68. errorUpload(response, file, fileList) {
  69. console.log("errorUpload", response, file, fileList);
  70. },
  71. exceed(files, fileList) {
  72. console.log('超过', files, fileList)
  73. }
  74. }
  75. }
  76. </script>
  77. <style scoped>
  78. </style>