copyPaste.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <template>
  2. <div>
  3. <el-button id="pasteUpload" type="primary" style="display: none" @click.stop="pasteUpload">upload</el-button>
  4. <normal-dialog
  5. :show-dialog="showCopyFile"
  6. :title="'上传截图'"
  7. :width="'40%'"
  8. :submit-button="'上传'"
  9. :top="'5vh'"
  10. @confirm="confirmUpload()"
  11. @cancel="showCopyFile=false"
  12. >
  13. <div class="file-dialog">
  14. <el-form ref="imageForm" label-width="20%" :rules="imageRules" :model="imageName">
  15. <el-form-item label="图片命名" prop="name">
  16. <el-col style="width: 75%">
  17. <el-input v-model="imageName.name" placeholder="请输入图片名称" />
  18. </el-col>
  19. <el-col style="width: 10%">.png</el-col>
  20. </el-form-item>
  21. </el-form>
  22. <div class="image">
  23. <div class="image-center">
  24. <img :src="imageUrl" class="image-url">
  25. </div>
  26. </div>
  27. </div>
  28. </normal-dialog>
  29. </div>
  30. </template>
  31. <script>
  32. import normalDialog from '@/components/dialog/normalDialog'
  33. import axios from 'axios'
  34. export default {
  35. components: {
  36. normalDialog
  37. },
  38. props: {
  39. id: {
  40. type: String,
  41. default: null,
  42. required: false
  43. }
  44. },
  45. data() {
  46. return {
  47. imageRules: {// 文件上传规则
  48. name: [
  49. { required: true, message: '请输入图片名称', trigger: 'blur' },
  50. { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
  51. ]
  52. },
  53. showCopyFile: false, // 复制文件对话框
  54. imageName: { name: null }, // 复制文件对象
  55. imageUrl: null // 图片地址
  56. }
  57. },
  58. created() {
  59. },
  60. mounted() {
  61. this.lisenPaste()
  62. },
  63. methods: {
  64. lisenPaste() { // 监听粘贴事件
  65. let contain
  66. console.log(this.id)
  67. if (this.id) {
  68. contain = document.getElementById(this.id)
  69. } else {
  70. contain = document.body
  71. }
  72. contain.onpaste = function(event) {
  73. event.stopImmediatePropagation()
  74. const data = (event.clipboardData || window.clipboardData)
  75. const items = data.items
  76. const fileList = [] // 存储文件数据
  77. if (items && items.length) { // 检索剪切板items
  78. for (let i = 0; i < items.length; i++) { // items[i].getAsFile() 想要的文件
  79. fileList.push(items[i].getAsFile())
  80. window.uploadFiles = fileList
  81. }
  82. document.getElementById('pasteUpload').click()
  83. }
  84. }
  85. },
  86. pasteUpload() { //
  87. if (!window.uploadFiles[0]) return
  88. const reader = new FileReader()
  89. reader.readAsDataURL(window.uploadFiles[0])
  90. reader.onload = () => {
  91. const reg = new RegExp(/image\/png/)
  92. this.imageUrl = reader.result
  93. if (this.imageUrl.match(reg)) { // 判断是否是图片
  94. this.showCopyFile = true
  95. this.imageName.name = this.generateMixed(10)
  96. }
  97. }
  98. },
  99. generateMixed(len) { // 图片随机名字生成
  100. const chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
  101. let res = ''
  102. for (let i = 0; i < len; i++) {
  103. const id = Math.ceil(Math.random() * 35)
  104. res += chars[id]
  105. }
  106. return res
  107. },
  108. async confirmUpload() {
  109. if (this.imageName.name === null || this.imageName.name.replace(/\s+/g, '') === '') {
  110. return false
  111. }
  112. this.showCopyFile = false
  113. const res = await this.updateFile(window.uploadFiles[0])
  114. const data = res.data
  115. const item = {
  116. name: `${this.imageName.name}.png` || `${this.generateMixed(10)}.png`,
  117. status: 'success',
  118. url: 'http:' + data.url
  119. }
  120. this.$emit('uploadFile', item)
  121. this.$message({
  122. showClose: true,
  123. message: '文件上传成功',
  124. type: 'success'
  125. })
  126. this.imageName.name = null
  127. this.imageUrl = null
  128. window.uploadFiles = null
  129. },
  130. updateFile(file) {
  131. const param = new FormData() // 创建form对象
  132. param.append('file', file)// 通过append向form对象添加数据
  133. const config = {
  134. headers: {
  135. 'Content-Type': 'multipart/form-data'
  136. },
  137. withCredentials: false
  138. } // 添加请求头
  139. return new Promise((resolve, reject) => {
  140. axios.post('http://star.xiaojukeji.com/upload/img.node', param, config)
  141. .then(response => {
  142. resolve(response)
  143. }).catch(err => {
  144. reject(err)
  145. })
  146. })
  147. }
  148. }
  149. }
  150. </script>
  151. <style scoped lang="scss">
  152. .file-dialog {
  153. display: flex;
  154. flex-direction: column;
  155. align-items: center;
  156. .el-form {
  157. width: 100%;
  158. }
  159. .image {
  160. position: relative;
  161. width: 61%;
  162. padding-top: 60%;
  163. border:1px solid #409EFF;
  164. border-radius: 4px;
  165. .image-center {
  166. padding: 1%;
  167. position: absolute;
  168. top: 0;
  169. left: 0;
  170. width: 100%;
  171. height: 100%;
  172. overflow-x: hidden;
  173. display: flex;
  174. justify-content: center;
  175. }
  176. .image-url {
  177. height: 100%;
  178. }
  179. }
  180. }
  181. </style>