Upload.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <div>
  3. <div class="crumbs">
  4. <el-breadcrumb separator="/">
  5. <el-breadcrumb-item><i class="el-icon-date"></i> 表单</el-breadcrumb-item>
  6. <el-breadcrumb-item>图片上传</el-breadcrumb-item>
  7. </el-breadcrumb>
  8. </div>
  9. <div class="container">
  10. <div class="content-title">支持拖拽</div>
  11. <div class="plugins-tips">
  12. Element UI自带上传组件。
  13. 访问地址:<a href="http://element.eleme.io/#/zh-CN/component/upload" target="_blank">Element UI Upload</a>
  14. </div>
  15. <el-upload
  16. class="upload-demo"
  17. drag
  18. action="/api/posts/"
  19. multiple>
  20. <i class="el-icon-upload"></i>
  21. <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  22. <div class="el-upload__tip" slot="tip">只能上传jpg/png文件,且不超过500kb</div>
  23. </el-upload>
  24. <div class="content-title">支持裁剪</div>
  25. <div class="plugins-tips">
  26. vue-cropperjs:一个封装了 cropperjs 的 Vue 组件。
  27. 访问地址:<a href="https://github.com/Agontuk/vue-cropperjs" target="_blank">vue-cropperjs</a>
  28. </div>
  29. <div class="crop-demo">
  30. <img :src="cropImg" class="pre-img">
  31. <div class="crop-demo-btn">选择图片
  32. <input class="crop-input" type="file" name="image" accept="image/*" @change="setImage"/>
  33. </div>
  34. </div>
  35. <el-dialog title="裁剪图片" :visible.sync="dialogVisible" width="30%">
  36. <vue-cropper ref='cropper' :src="imgSrc" :ready="cropImage" :zoom="cropImage" :cropmove="cropImage" style="width:100%;height:300px;"></vue-cropper>
  37. <span slot="footer" class="dialog-footer">
  38. <el-button @click="cancelCrop">取 消</el-button>
  39. <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  40. </span>
  41. </el-dialog>
  42. </div>
  43. </div>
  44. </template>
  45. <script>
  46. import VueCropper from 'vue-cropperjs';
  47. export default {
  48. name: 'upload',
  49. data: function(){
  50. return {
  51. defaultSrc: './static/img/img.jpg',
  52. fileList: [],
  53. imgSrc: '',
  54. cropImg: '',
  55. dialogVisible: false,
  56. }
  57. },
  58. components: {
  59. VueCropper
  60. },
  61. methods:{
  62. setImage(e){
  63. const file = e.target.files[0];
  64. if (!file.type.includes('image/')) {
  65. return;
  66. }
  67. const reader = new FileReader();
  68. reader.onload = (event) => {
  69. this.dialogVisible = true;
  70. this.imgSrc = event.target.result;
  71. this.$refs.cropper && this.$refs.cropper.replace(event.target.result);
  72. };
  73. reader.readAsDataURL(file);
  74. },
  75. cropImage () {
  76. this.cropImg = this.$refs.cropper.getCroppedCanvas().toDataURL();
  77. },
  78. cancelCrop(){
  79. this.dialogVisible = false;
  80. this.cropImg = this.defaultSrc;
  81. },
  82. imageuploaded(res) {
  83. console.log(res)
  84. },
  85. handleError(){
  86. this.$notify.error({
  87. title: '上传失败',
  88. message: '图片上传接口上传失败,可更改为自己的服务器接口'
  89. });
  90. }
  91. },
  92. created(){
  93. this.cropImg = this.defaultSrc;
  94. }
  95. }
  96. </script>
  97. <style scoped>
  98. .content-title{
  99. font-weight: 400;
  100. line-height: 50px;
  101. margin: 10px 0;
  102. font-size: 22px;
  103. color: #1f2f3d;
  104. }
  105. .pre-img{
  106. width: 100px;
  107. height: 100px;
  108. background: #f8f8f8;
  109. border: 1px solid #eee;
  110. border-radius: 5px;
  111. }
  112. .crop-demo{
  113. display: flex;
  114. align-items: flex-end;
  115. }
  116. .crop-demo-btn{
  117. position: relative;
  118. width: 100px;
  119. height: 40px;
  120. line-height: 40px;
  121. padding: 0 20px;
  122. margin-left: 30px;
  123. background-color: #409eff;
  124. color: #fff;
  125. font-size: 14px;
  126. border-radius: 4px;
  127. box-sizing: border-box;
  128. }
  129. .crop-input{
  130. position: absolute;
  131. width: 100px;
  132. height: 40px;
  133. left: 0;
  134. top: 0;
  135. opacity: 0;
  136. cursor: pointer;
  137. }
  138. </style>