uploadFiles.vue 6.0 KB

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