uploadFiles.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <!--
  2. 上传组件
  3. type: 类型,默认image
  4. identify: key值,通过val获取
  5. disabled: 是否可用
  6. index: 父组件的下标
  7. imageIndex: 图片下标
  8. isShow: 图片的显示
  9. -->
  10. <template>
  11. <div id="saga-upload">
  12. <div id="uploadFile">
  13. <div v-if="item && filesArr.length" v-for="(item,index) in filesArr">
  14. <el-button type="text" @click="download(item)">{{delFile(item)}}</el-button>
  15. <i
  16. v-if="!readOnly"
  17. class="el-icon-close delete-icon"
  18. style="margin-left:10px; cursor:pointer"
  19. @click="deleteFile(index,item)"
  20. ></i>
  21. </div>
  22. <el-upload
  23. v-if="filesArr.length < max"
  24. class="upload-file"
  25. action
  26. :http-request="uploadAndSubmit"
  27. :show-file-list="false"
  28. drag
  29. >
  30. <el-button size="small" type="primary" v-if="!readOnly">点击上传</el-button>
  31. <div slot="tip" class="el-upload__tip" v-if="!readOnly">请上传文件</div>
  32. </el-upload>
  33. </div>
  34. </div>
  35. </template>
  36. <script>
  37. import tools from "@/utils/scan/tools";
  38. export default {
  39. props: {
  40. keysArr: {
  41. type: [Array, String],
  42. default: function () {
  43. return []
  44. }
  45. },
  46. readOnly: {
  47. type: Boolean,
  48. default: false
  49. },
  50. max: {
  51. type: [Number, String],
  52. default: 6
  53. },
  54. defined: null
  55. },
  56. data() {
  57. return {
  58. filesArr: []
  59. };
  60. },
  61. created() {
  62. let type = typeof (this.keysArr)
  63. this.fileFalg()
  64. },
  65. methods: {
  66. //判断是否为空
  67. fileFalg() {
  68. let type = typeof (this.keysArr)
  69. console.log(this.keysArr, "keysAee")
  70. if (type == 'string') {
  71. this.filesArr = [this.keysArr]
  72. } else {
  73. this.filesArr = tools.deepCopy(this.keysArr)
  74. }
  75. if (!this.keysArr) {
  76. this.filesArr = []
  77. }
  78. },
  79. //处理地址
  80. delFile(name) {
  81. console.log(name, "name")
  82. return name.length > 20 ? name.substring(0, 20) + "..." : ""
  83. },
  84. resetFile() {
  85. this.filesArr = []
  86. },
  87. //点击下载
  88. download(key) {
  89. console.log(key)
  90. window.open("/image-service/common/file_get/" + key + "?systemId=dataPlatform")
  91. },
  92. //删除图片
  93. deleteFile(i, key) {
  94. this.filesArr.splice(i, 1);
  95. this.$emit("change", this.filesArr, this.defined);
  96. },
  97. //上传
  98. uploadAndSubmit(item) {
  99. // var form = document.forms["demoForm"];
  100. // if (form["file"].files.length > 0) {
  101. // 寻找表单域中的 <input type="file" ... /> 标签
  102. // var file = form["file"].files[0];
  103. let file = item.file;
  104. // try sending
  105. let reader = new FileReader();
  106. let vm = this;
  107. let fileType = file.name.split(".");
  108. let type = fileType[fileType.length - 1];
  109. let key = "&key=" + fileType[0] + file.uid + "." + type
  110. reader.onloadstart = function () {
  111. // 这个事件在读取开始时触发
  112. };
  113. reader.onprogress = function (p) {
  114. // 这个事件在读取进行中定时触发
  115. };
  116. reader.onload = function () {
  117. // 这个事件在读取成功结束后触发
  118. };
  119. reader.onloadend = function () {
  120. // 这个事件在读取结束后,无论成功或者失败都会触发
  121. if (reader.error) {
  122. } else {
  123. // document.getElementById("bytesRead").textContent = file.size;
  124. // 构造 XMLHttpRequest 对象,发送文件 Binary 数据
  125. var xhr = new XMLHttpRequest();
  126. xhr.open(
  127. /* method */
  128. "POST",
  129. /* target url */
  130. "/img/image-service/common/file_upload?systemId=dataPlatform&secret=9e0891a7a8c8e885&overwrite=true" + key
  131. /*, async, default to true */
  132. );
  133. //xhr.overrideMimeType("application/octet-stream");
  134. xhr.send(reader.result);
  135. xhr.onreadystatechange = function () {
  136. if (xhr.readyState == 4) {
  137. console.log(xhr)
  138. if (xhr.status == 200) {
  139. vm.filesArr.push(
  140. key.split("=")[1]
  141. );
  142. console.log(vm.filesArr)
  143. vm.$emit("change", vm.filesArr, vm.defined);
  144. } else {
  145. this.$message.error(res.data.ResultMsg)
  146. }
  147. }
  148. };
  149. }
  150. };
  151. reader.readAsArrayBuffer(file);
  152. }
  153. },
  154. watch: {
  155. keysArr: function (val) {
  156. this.fileFalg()
  157. }
  158. }
  159. };
  160. </script>
  161. <style lang="less">
  162. #saga-upload {
  163. .dill-image {
  164. position: absolute;
  165. right: 0px;
  166. top: 0px;
  167. font-size: 20px;
  168. }
  169. .el-upload-dragger {
  170. width: 180px;
  171. height: 180px;
  172. }
  173. img {
  174. position: absolute;
  175. top: 0;
  176. bottom: 0;
  177. left: 0;
  178. right: 0;
  179. width: 100%;
  180. height: 100%;
  181. }
  182. #uploadFile {
  183. .upload-file {
  184. overflow: hidden;
  185. .el-upload-dragger {
  186. width: inherit;
  187. height: inherit;
  188. border: none;
  189. }
  190. }
  191. }
  192. }
  193. </style>