人民医院前端

uploadFile.vue 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <view class="wrapper" style="background-color: #fff;">
  3. <view class="form" style="margin-left: -10px;">
  4. <lsj-upload ref="lsjUpload" childId="upload" :option="option"
  5. :debug="true" :instantly="true" @progress="onprogress" @change="onChange">
  6. <button class="mini-btn" type="primary" size="mini">选择附件</button>
  7. </lsj-upload>
  8. <view v-if="fileUrlList" v-for="(item,index) in fileUrlList" :key="index" style="padding: 7px 13px;border: 1px solid rgb(238,238,238);color: rgb(102,102,102);">
  9. <text style="width: 80%;display: inline-block;word-break: break-all">{{item.name}}</text>
  10. <text @click="clear(item.id,item.name)" style="margin-left: 10rpx;padding: 0 10rpx;float: right;">X</text>
  11. </view>
  12. <!-- #ifndef MP-WEIXIN -->
  13. <view v-for="(item,index) in files.values()" :key="'i' + index" style="padding: 7px 13px;border: 1px solid rgb(238,238,238);color: rgb(102,102,102);">
  14. <text style="width: 80%;display: inline-block;word-break: break-all">{{item.name}}</text>
  15. <text @click="clear(JSON.parse(item.responseText).data[0].F_FileId,item.name)" style="margin-left: 10rpx;padding: 0 10rpx;float: right;">X</text>
  16. </view>
  17. <!-- #endif -->
  18. </view>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. import indexConfig from '@/config/index.config';
  24. export default {
  25. data() {
  26. return {
  27. fileIdList: [],
  28. option: {
  29. url: indexConfig.baseUrl + "/FaultRepair/UploadFile",
  30. name: 'file',
  31. header: {
  32. Authorization: uni.getStorageSync('Admin-Token')
  33. }
  34. },
  35. files: new Map(),
  36. // 微信小程序Map对象for循环不显示,所以转成普通数组,不要问为什么,我也不知道
  37. wxFiles: [],
  38. }
  39. },
  40. props: {
  41. fileUrlList: {
  42. type: Array
  43. }
  44. },
  45. watch: {
  46. 'fileUrlList': function(){
  47. this.fileUrlList.forEach((v,n) => {
  48. this.fileIdList.push(v.id)
  49. })
  50. }
  51. },
  52. onLoad() {
  53. },
  54. methods: {
  55. // 上传进度回调
  56. onprogress(item) {
  57. // 更新当前状态变化的文件
  58. this.files.set(item.name, item);
  59. if(item.type == 'success') {
  60. this.fileIdList.push(JSON.parse(item.responseText).data[0].F_FileId)
  61. this.getImgData(this.fileIdList)
  62. }
  63. console.log(typeof(this.files.values()))
  64. // 微信小程序Map对象for循环不显示,所以转成普通数组,不要问为什么,我也不知道
  65. // #ifdef MP-WEIXIN
  66. this.wxFiles = [...this.files.values()];
  67. // #endif
  68. // 强制更新视图
  69. this.$forceUpdate();
  70. },
  71. getImgData(data){
  72. let newdata = data.filter(function(item,index) {
  73. return data.indexOf(item) === index
  74. })
  75. this.$emit('post-string-data',newdata)
  76. },
  77. // 文件选择回调
  78. onChange(files) {
  79. // 更新选择的文件
  80. this.files = files;
  81. // 强制更新视图
  82. this.$forceUpdate();
  83. // 微信小程序Map对象for循环不显示,所以转成普通数组,不要问为什么,我也不知道
  84. // #ifdef MP-WEIXIN
  85. this.wxFiles = [...this.files.values()];
  86. // #endif
  87. },
  88. // 手动上传
  89. upload() {
  90. // name=指定文件名,不指定则上传所有type等于waiting和fail的文件
  91. this.$refs.lsjUpload.upload();
  92. },
  93. // 移除某个文件
  94. clear(data,name) {
  95. this.fileIdList.forEach((v,n) => {
  96. if(data === v) {
  97. this.fileUrlList.splice(n,1)
  98. this.fileIdList.splice(n,1)
  99. }
  100. })
  101. this.getImgData(this.fileIdList)
  102. // name=指定文件名,不传name默认移除所有文件
  103. this.$refs.lsjUpload.clear(name);
  104. },
  105. // 打开nvue窗口
  106. open() {
  107. uni.navigateTo({
  108. url: '/pages/nvue-demo/nvue-demo'
  109. });
  110. }
  111. }
  112. }
  113. </script>
  114. <style>
  115. .example-body {
  116. padding: 10px;
  117. padding-top: 0;
  118. }
  119. .custom-image-box {
  120. /* #ifndef APP-NVUE */
  121. display: flex;
  122. /* #endif */
  123. flex-direction: row;
  124. justify-content: space-between;
  125. align-items: center;
  126. }
  127. .text {
  128. font-size: 14px;
  129. color: #333;
  130. }
  131. .item__label {
  132. display: flex;
  133. flex-shrink: 0;
  134. box-sizing: border-box;
  135. flex-direction: row;
  136. align-items: center;
  137. width: 65px;
  138. padding: 5px 0;
  139. height: 36px;
  140. width: 70px;
  141. justify-content: flex-start;
  142. float: left;
  143. }
  144. </style>