uploadPar.vue 7.8 KB

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