upload.vue 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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"/></span>
  9. <a @click="showImg(qualifications.url)"><i class="fa fa-search"/>查看</a>
  10. </div>
  11. <div id="image-box" v-if="isShow">
  12. <div class="x-floating-wrap"/>
  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'],
  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) {
  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. let param = new FormData()
  46. param.append('file', file, file.name)
  47. param.append('chunk', '0')
  48. if (file.type !== 'application/pdf') {
  49. let config = {
  50. headers: {'Content-Type': 'multipart/form-data'}
  51. }
  52. this.$http.post('/api/images', param, config)
  53. .then(response => {
  54. this.isPdf = false
  55. this.qualifications.url = response.data[0].path
  56. this.qualifications.type = typeof this.typeData === 'undefined' ? '' : this.typeData
  57. this.$emit('uploadAction', this.qualifications)
  58. })
  59. } else {
  60. let config = {
  61. headers: {'Content-Type': file.type}
  62. }
  63. this.$http.post('/file', param, config)
  64. .then(response => {
  65. this.isPdf = true
  66. this.qualifications.url = response.data[0].path
  67. this.qualifications.type = typeof this.typeData === 'undefined' ? '' : this.typeData
  68. this.$emit('uploadAction', this.qualifications)
  69. }, err => {
  70. console.log(err)
  71. })
  72. }
  73. },
  74. showImg (imgUrl) {
  75. if (!this.isPdf) {
  76. this.qualifications.url = imgUrl
  77. this.isShow = true
  78. } else {
  79. window.open(imgUrl)
  80. }
  81. },
  82. deleteImg () {
  83. this.qualifications.url = ''
  84. this.qualifications.type = this.typeData
  85. this.$emit('uploadAction', this.qualifications)
  86. }
  87. }
  88. }
  89. </script>
  90. <style>
  91. </style>