upload.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <div>
  3. <div class="preview">
  4. <img :src="qualifications.url==''?'/images/all/upload-apply.png': isPdf?'/images/all/timg.png':qualifications.url" class="previewImage"/>
  5. <input type="file" v-if="!qualifications.url" class="com-input" @change="update" accept="image/jpeg,image/jpg,image/gif,image/bmp,image/png,.pdf" />
  6. </div>
  7. <div class="hover-show" v-if="qualifications.url">
  8. <span class="delete" title="删除" @click="deleteImg(qualifications.url)"><i class="fa fa-trash"></i></span>
  9. <a @click="showImg(qualifications.url)"><i class="fa fa-search"></i>查看</a>
  10. </div>
  11. <div id="image-box" v-if="isShow">
  12. <div class="x-floating-wrap"></div>
  13. <div class="x-floating">
  14. <div id="item-content">
  15. <div class="x-close-wrap" @click="isShow = false"><a href="javascript:void(0);">&times;</a></div>
  16. <div class="img"><img :src="qualifications.url"/></div>
  17. </div>
  18. </div>
  19. </div>
  20. </div>
  21. </template>
  22. <script>
  23. export default {
  24. props: ['typeData', 'url', 'NopassThree'],
  25. data () {
  26. return {
  27. qualifications: {
  28. url: '',
  29. type: ''
  30. },
  31. isShow: false,
  32. isPdf: false
  33. }
  34. },
  35. watch: {
  36. url: function (val, oldVal) {
  37. if (val && val !== '') {
  38. this.qualifications.url = val
  39. }
  40. }
  41. },
  42. methods: {
  43. update (e) {
  44. let file = e.target.files[0]
  45. if (this.NopassThree) {
  46. console.log(file.size)
  47. if (file.size > 3 * 1024 * 1024) {
  48. this.$message.error('上传文件不得超过3M')
  49. return false
  50. }
  51. }
  52. let param = new FormData()
  53. param.append('file', file, file.name)
  54. param.append('chunk', '0')
  55. if (file.type !== 'application/pdf') {
  56. let config = {
  57. headers: {'Content-Type': 'multipart/form-data'}
  58. }
  59. this.$http.post('/api/images', param, config)
  60. .then(response => {
  61. this.isPdf = false
  62. this.qualifications.url = response.data[0].path
  63. this.qualifications.type = typeof this.typeData === 'undefined' ? '' : this.typeData
  64. this.$emit('uploadAction', this.qualifications)
  65. })
  66. } else {
  67. let config = {
  68. headers: {'Content-Type': file.type}
  69. }
  70. this.$http.post('/file', param, config)
  71. .then(response => {
  72. this.isPdf = true
  73. this.qualifications.url = response.data[0].path
  74. this.qualifications.type = typeof this.typeData === 'undefined' ? '' : this.typeData
  75. this.$emit('uploadAction', this.qualifications)
  76. }, err => {
  77. console.log(err)
  78. })
  79. }
  80. },
  81. showImg (imgUrl) {
  82. if (!this.isPdf) {
  83. this.qualifications.url = imgUrl
  84. this.isShow = true
  85. } else {
  86. window.open(imgUrl)
  87. }
  88. },
  89. deleteImg () {
  90. this.qualifications.url = ''
  91. this.qualifications.type = this.typeData
  92. this.$emit('uploadAction', this.qualifications)
  93. }
  94. }
  95. }
  96. </script>