uploadFiles.vue 6.5 KB

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