upload.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <template>
  2. <div>
  3. <el-upload
  4. class="upload"
  5. ref="upload"
  6. action=""
  7. :before-upload="beforeUpload"
  8. :on-change="handleChange"
  9. :on-remove="handleRemove"
  10. :on-preview="preview"
  11. :on-success="success"
  12. :on-error="error"
  13. :http-request="toUpload"
  14. :auto-upload="false"
  15. :file-list="fileList"
  16. :on-exceed="handleExceed"
  17. :multiple="multiple"
  18. >
  19. <el-button type="primary" plain icon="el-icon-upload"
  20. >上传文件</el-button
  21. >
  22. </el-upload>
  23. <!-- <el-button @click="download">下载</el-button>-->
  24. </div>
  25. </template>
  26. <script>
  27. export default {
  28. name: "upload",
  29. props: {
  30. // 限制上传的个数
  31. limit: {
  32. type: Number,
  33. default: 1,
  34. },
  35. // 一组文件传相同的值,保证其为一组
  36. objectPath: {
  37. type: String,
  38. default: "1",
  39. },
  40. type: {
  41. type: String,
  42. default: "4",
  43. },
  44. // 是否需要处理大文件上传
  45. needInstance: {
  46. type: Boolean,
  47. default: false,
  48. },
  49. // 上传url
  50. uploadUrl: {
  51. type: String,
  52. default: "",
  53. },
  54. multiple: {
  55. type: Boolean,
  56. default: false,
  57. },
  58. },
  59. data() {
  60. return {
  61. fileList: [],
  62. attachmentList: [],
  63. defaultParam: {},
  64. };
  65. },
  66. methods: {
  67. beforeUpload(file) {
  68. let arr = ["xls", "xlsx"];
  69. let i = file.name.lastIndexOf(".");
  70. if (!arr.includes(file.name.slice(i + 1))) {
  71. this.$message.warning("请上传以xlsx、xls为后缀名的Excel文件");
  72. return false;
  73. }
  74. },
  75. handleChange(file, fileList) {
  76. this.fileList = fileList;
  77. let arr = ["xls", "xlsx"];
  78. let i = file.name.lastIndexOf(".");
  79. if (!arr.includes(file.name.slice(i + 1))) {
  80. this.$message.warning("请上传以xlsx、xls为后缀名的Excel文件");
  81. return false;
  82. }
  83. this.$emit("handleChange", file);
  84. },
  85. handleExceed(files, fileList) {
  86. this.$message.warning(
  87. `当前限制选择 ${this.limit} 个文件,本次选择了 ${
  88. files.length
  89. } 个文件,共选择了 ${files.length + fileList.length} 个文件`
  90. );
  91. },
  92. async handleRemove(file, fileList) {
  93. let md5 = "";
  94. if (file.raw) {
  95. await this.$md5(file.raw).then((res) => {
  96. md5 = res;
  97. });
  98. } else {
  99. md5 = file.md5;
  100. }
  101. let removeIndex = "";
  102. this.attachmentList.forEach((item, index) => {
  103. if (md5 === item.md5) {
  104. removeIndex = index;
  105. }
  106. });
  107. // if (removeIndex < 0) {
  108. // this.$message.error("删除失败");
  109. // }
  110. this.attachmentList.splice(removeIndex, 1);
  111. this.$emit("attachmentChange", this.attachmentList);
  112. },
  113. async toUpload(file) {
  114. let _this = this;
  115. // console.log(file);
  116. return;
  117. return new Promise(async (resolve, reject) => {
  118. let formData = new FormData();
  119. await formData.append("name", file.file.name);
  120. // await formData.append('type', _this.defaultParam.md5)
  121. // console.log(this);
  122. await formData.append("type", this.type);
  123. await formData.append("objectPath", Math.random().toString());
  124. // await formData.append("md5", _this.defaultParam.md5);
  125. await formData.append("file", file.file);
  126. let axios = this.$axios;
  127. // 处理大文件上传 instance已被设置不携带token
  128. // if (this.needInstance) {
  129. // axios = this.$instance;
  130. // }
  131. // 上传前进行通知
  132. this.$emit("willUpload");
  133. /* let url = this.uploadUrl;
  134. let defaultParam = {};
  135. if (!url) {
  136. url = this.$api.common.uploadWj;
  137. // 获取MD5值
  138. await this.$md5(file.file).then((res) => {
  139. defaultParam = {
  140. // file: file.file,
  141. md5: res,
  142. };
  143. });
  144. } */
  145. let url = this.$api.sceneLibrary.saveSceneNatural;
  146. let defaultParam = {};
  147. // 获取MD5值
  148. await this.$md5(file.file).then((res) => {
  149. defaultParam = {
  150. // file: file.file,
  151. md5: res,
  152. };
  153. });
  154. await axios({
  155. method: "post",
  156. url,
  157. data: formData,
  158. withCredentials: true,
  159. headers: {
  160. "Content-type": "multipart/form-data",
  161. },
  162. }).then((res) => {
  163. if (res.code == 200) {
  164. // this.attachmentList.push({
  165. // // attachmentName: file.file.name,
  166. // // attachmentAddress: res.params,
  167. // md5: defaultParam.md5 || "",
  168. // // uid: file.file.uid,
  169. // fileType: file.file.type,
  170. // // fileName: res.info.fileName,
  171. // // videoPreview: res.info.videoPreview,
  172. // });
  173. // resolve("成功");
  174. // this.$emit("attachmentChange", this.attachmentList);
  175. // 上传完成后进行通知
  176. // this.$emit("didUpload", "success");
  177. this.$refs.upload.clearFiles();
  178. this.$emit("didUpload");
  179. } else {
  180. this.$message.error(res.message || "上传失败");
  181. // this.$emit("didUpload", "fail");
  182. // reject("失败");
  183. }
  184. });
  185. // .catch((err) => {
  186. // this.$emit("didUpload", "error");
  187. // reject(err);
  188. // });
  189. });
  190. },
  191. success(response, file, fileList) {
  192. this.$message.success("上传成功 ");
  193. },
  194. error(response, file, fileList) {
  195. this.$message.warning("上传失败");
  196. },
  197. preview(file) {
  198. return false;
  199. console.log(file);
  200. console.log(this.attachmentList);
  201. let item = this.attachmentList.find((item) =>
  202. item.fileName.endsWith(file.name)
  203. );
  204. this.download(item.fileName, item.attachmentName);
  205. },
  206. download(downPath, downName) {
  207. this.$axios({
  208. method: "post",
  209. url: this.$api.common.download,
  210. responseType: "blob",
  211. data: {
  212. objectName: downPath,
  213. },
  214. }).then((res) => {
  215. const blob = new Blob([res]); //构造一个blob对象来处理数据
  216. const fileName = downName;
  217. //对于<a>标签,只有 Firefox 和 Chrome(内核) 支持 download 属性
  218. //IE10以上支持blob但是依然不支持download
  219. if ("download" in document.createElement("a")) {
  220. //支持a标签download的浏览器
  221. const link = document.createElement("a"); //创建a标签
  222. link.download = fileName; //a标签添加属性
  223. link.style.display = "none";
  224. link.href = URL.createObjectURL(blob);
  225. document.body.appendChild(link);
  226. link.click(); //执行下载
  227. URL.revokeObjectURL(link.href); //释放url
  228. document.body.removeChild(link); //释放标签
  229. } else {
  230. //其他浏览器
  231. navigator.msSaveBlob(blob, fileName);
  232. }
  233. });
  234. },
  235. },
  236. };
  237. </script>
  238. <style scoped>
  239. .upload /deep/ .el-upload-list {
  240. display: none;
  241. }
  242. </style>