fileupload.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <template>
  2. <div class="webuploader-container">
  3. <div :id="'uploader'+upId">
  4. <el-row>
  5. <!-- <el-col :span="6"> {{name}} </el-col>
  6. <el-col :span="6"> <div :id="'picker'+upId" class="text_color" >选择文件</div> </el-col> -->
  7. <!-- <el-button :id="'picker'+upId"> {{name}} </el-button> -->
  8. <div class="btntext upname" :id="'picker'+upId">{{name}}</div>
  9. </el-row>
  10. <el-row v-for="(fileObj,key) in files" :key="key" class="disflex">
  11. <el-col :span="6">{{fileObj.name}} </el-col>
  12. <el-col :span="8"><el-progress :percentage="fileObj.percentage"></el-progress></el-col>
  13. <el-col :span="5">{{fileObj.msg}}</el-col>
  14. <el-col :span="5"><el-button size="mini" type="danger" @click="fileDel(fileObj.id)" icon="el-icon-delete" circle></el-button></el-col>
  15. </el-row>
  16. <!-- <el-button size="mini" @click="uploader.upload()">开始上传</el-button> -->
  17. </div>
  18. </div>
  19. </template>
  20. <script>
  21. import $ from 'jquery' ;
  22. import WebUploader from 'webuploader';
  23. import store from "@/store";
  24. import { request } from "@/utils/request";
  25. import { ElMessage } from '@/utils/message.js'
  26. import { log } from 'three';
  27. import { Message, MessageBox } from "element-ui";
  28. import { connect } from 'echarts/lib/echarts';
  29. // let Message = new ElMessage()
  30. export default {
  31. data(){
  32. return {
  33. uploader:undefined,
  34. fileName:undefined,
  35. md5:"1212",
  36. files:[],
  37. nmllist:[],// 批量上传的数组
  38. gridFileId:'',
  39. boundaryFileId:'',
  40. uuid:'',
  41. nmlid:'',
  42. upstate:'1', //状态
  43. hcfdBoundarys:[],
  44. }
  45. },
  46. updated(){
  47. // this.initWebUploader();
  48. },
  49. mounted(){
  50. this.initWebUploader();
  51. },
  52. props:{//传入参数
  53. projectId: String,
  54. solverType: String,
  55. accept:String,
  56. upId:String,
  57. name:String,
  58. namelist:Array,//文件名的集合
  59. },
  60. created(){
  61. this.getUUID()
  62. this.uuid= this.getUUID()
  63. },
  64. methods:{
  65. getUUID() {
  66. return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  67. var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
  68. return v.toString(16);
  69. })
  70. },
  71. //HCFDugrid绑定
  72. bindugrid(bfid){
  73. let params = {
  74. transCode: "D00001",
  75. pid: this.projectId,
  76. gridFileType:this.solverType,
  77. gridFileId:bfid,
  78. };
  79. request(params)
  80. .then((res) => {
  81. console.log("绑定成功")
  82. })
  83. .catch((err) => {});
  84. },
  85. //HCFDmapbc绑定
  86. bindmapbc(mapbcFileId){
  87. let solverConfigId= sessionStorage.getItem("solverConfigid");
  88. let params = {
  89. transCode: "D00002",
  90. solverConfigId: solverConfigId,
  91. mapbcFileId:mapbcFileId,
  92. };
  93. request(params)
  94. .then((res) => {
  95. console.log("绑定成功")
  96. this.mapbc();
  97. })
  98. .catch((err) => {});
  99. },
  100. //调用c0003
  101. mapbc(){
  102. let paramsdata=JSON.parse(sessionStorage.getItem("params"))
  103. request(paramsdata)
  104. .then((res) => {
  105. let data = JSON.parse(res.parameterObj);
  106. this.hcfdBoundarys= data.hcfdBoundarys
  107. this.$emit("changedesc",JSON.stringify(this.hcfdBoundarys));
  108. })
  109. .catch((err) => {
  110. })
  111. },
  112. initWebUploader(){
  113. this.files=[];
  114. var v_this=this;
  115. this.uploader=WebUploader.create({
  116. // swf文件路径
  117. swf: './Uploader.swf',
  118. // 文件接收服务端。
  119. server: process.env.VUE_APP_BASE_API+'/TransServlet',
  120. //文件上传请求的参数表,每次发送都会发送此对象中的参数。
  121. formData: {
  122. },
  123. // 选择文件的按钮。可选。
  124. // 内部根据当前运行是创建,可能是input元素,也可能是flash.
  125. pick: {
  126. id:'#picker'+v_this.upId,
  127. multiple:true
  128. },
  129. // 不压缩image, 默认如果是jpeg,文件上传前会压缩一把再上传!
  130. resize: false,
  131. // 开起分片上传。
  132. chunked: true,
  133. chunkSize: 2 * 1024 * 1024,
  134. threads :4,
  135. })
  136. // 当有文件被添加进队列的时候
  137. this.uploader.on( 'fileQueued', file=> {
  138. v_this.fileName=file.name;
  139. // console.log( v_this.fileName);
  140. if(this.upId=='1'||this.upId=='2'||this.upId=='3'){
  141. const fax=v_this.fileName.split(".")[1];
  142. if(v_this.accept.indexOf(fax)<0){//判断上传支持文件类型
  143. v_this.uploader.reset();
  144. Message.error("只支持" +v_this.accept+"格式文件!");
  145. return;
  146. }
  147. v_this.uploader.md5File(file)
  148. .progress(percentage=> {//显示加密进度
  149. console.log('Percentage:', percentage);
  150. })
  151. .then(md5 =>{ // 完成
  152. v_this.md5=md5;
  153. });
  154. if(v_this.files.length>0){
  155. v_this.uploader.removeFile(v_this.files[0].id);
  156. v_this.files=[];
  157. }
  158. v_this.files.push({
  159. id:file.id,
  160. name:file.name,
  161. msg:"等待上传...",
  162. percentage:0
  163. })
  164. this.uploader.upload();
  165. }
  166. else{
  167. if(v_this.namelist.includes(v_this.fileName)){
  168. v_this.uploader.reset();
  169. Message.error("文件名已存在请修改文件名才能上传");
  170. return;
  171. }
  172. this.namelist.push(v_this.fileName);
  173. const fax=v_this.fileName.split(".")[1];
  174. if(v_this.accept.indexOf(fax)<0){//判断上传支持文件类型
  175. v_this.uploader.reset();
  176. Message.error("只支持" +v_this.accept+"格式文件!");
  177. return;
  178. }
  179. v_this.uploader.md5File(file)
  180. .progress(percentage=> {//显示加密进度
  181. console.log('Percentage:', percentage);
  182. })
  183. .then(md5 =>{ // 完成
  184. v_this.md5=md5;
  185. });
  186. if(v_this.files.length>0){
  187. v_this.uploader.removeFile(v_this.files[0].id);
  188. v_this.files=[];
  189. }
  190. v_this.files.push({
  191. id:file.id,
  192. name:file.name,
  193. msg:"等待上传...",
  194. percentage:0
  195. })
  196. this.uploader.upload();
  197. // }
  198. }
  199. });
  200. /**文件发送前
  201. * object {Object}
  202. data {Object}默认的上传参数,可以扩展此对象来控制上传参数。
  203. headers {Object}可以扩展此对象来控制上传头部
  204. */
  205. this.uploader.on('uploadBeforeSend', (file,data,headers)=> {
  206. data.chunk=file.chunk;
  207. data.chunks=file.chunks;
  208. data.bfid=v_this.uuid;
  209. data.fileName=v_this.fileName;
  210. // data.pid=v_this.projectId;
  211. // data.solverType=v_this.solverType;
  212. // data.md5=v_this.md5;
  213. // data.uploadFlag=2;//1 普通 2覆盖 3续传
  214. data.channelNo ='service';
  215. data.clientToken=store.getters.token;
  216. data.transCode='B00028';
  217. data.userId=store.getters.userId;
  218. headers.Content='multipart';
  219. headers.Type='form-data';
  220. })
  221. // 文件上传过程中创建进度条实时显示。
  222. this.uploader.on( 'uploadProgress', ( file, percentage ) =>{
  223. let prage= Math.floor(percentage*100);
  224. // v_this.$emit('childFn',{name:v_this.name,percentage:prage})
  225. v_this.files.forEach(fileObj=>{
  226. if(fileObj.id=file.id)
  227. fileObj.msg='上传中';
  228. fileObj.percentage=prage;
  229. })
  230. this.upstate='2';
  231. sessionStorage.setItem('upstate', this.upstate)
  232. });
  233. this.uploader.on( 'uploadSuccess', ( file ,response) =>{
  234. console.log(response);
  235. if(file.ext=='ugrid'){
  236. this.gridFileId=response.bfid;
  237. let params = {
  238. transCode: "B00029",
  239. bfid: response.bfid,
  240. };
  241. request(params)
  242. .then((res) => {
  243. sessionStorage.removeItem('upstate');
  244. console.log(res)
  245. v_this.files.forEach((fileObj) => {
  246. if ((fileObj.id = file.id)){
  247. fileObj.msg = "已上传";
  248. v_this.$emit('upoK')
  249. }
  250. v_this.bindugrid(response.bfid);
  251. });
  252. })
  253. .catch((err) => {});
  254. }else if(file.ext=='mapbc'){
  255. let params = {
  256. transCode: "B00029",
  257. bfid: response.bfid,
  258. };
  259. request(params)
  260. .then((res) => {
  261. sessionStorage.removeItem('upstate');
  262. console.log(res)
  263. v_this.files.forEach((fileObj) => {
  264. if ((fileObj.id = file.id)){
  265. fileObj.msg = "已上传";
  266. v_this.$emit('upoK')
  267. }
  268. v_this.bindmapbc(response.bfid);
  269. });
  270. })
  271. .catch((err) => {});
  272. }else{
  273. }
  274. });
  275. //
  276. /**
  277. * 当某个文件上传到服务端响应后,会派送此事件来询问服务端响应是否有效。如果此事件handler返回值为false, 则此文件将派送server类型的uploadError事件。
  278. * object {Object}
  279. ret {Object}服务端的返回数据,json格式,如果服务端不是json格式,从ret._raw中取数据,自行解析
  280. */
  281. this.uploader.on('uploadAccept',(object,res)=>{
  282. if(res.returnCode=="000000000"){
  283. return true;
  284. }else{
  285. Message.error(res.returnMsg);
  286. return false;
  287. }
  288. });
  289. this.uploader.on( 'uploadError', ( file ,reason)=> {
  290. sessionStorage.removeItem('upstate');
  291. $( '#'+file.id ).text(file.name+'上传出错');
  292. this.getUUID();
  293. this.uuid= this.getUUID();
  294. });
  295. this.uploader.on('uploadComplete', file => {
  296. // $( '#'+file.id ).find('.progress').fadeOut();
  297. });
  298. },
  299. uploaderClear(){
  300. this.uploader.reset();
  301. this.percentage=0;
  302. this.files=[];
  303. },
  304. fileDel(fileId){
  305. // if(sessionStorage.getItem('upstate')=='2'){
  306. // Message({
  307. // type: "error",
  308. // message: "上传中不能删除",
  309. // });
  310. // }else{
  311. if(this.upId=='1'||this.upId=='2'||this.upId=='3'){
  312. this.getUUID();
  313. this.uuid= this.getUUID();
  314. sessionStorage.removeItem('upstate');
  315. this.files=this.files.filter(fileObj=>{
  316. if( fileObj.id == fileId){
  317. console.log(fileObj);
  318. this.uploader.removeFile(fileId);
  319. }
  320. return fileObj.id != fileId
  321. })
  322. }else{
  323. sessionStorage.removeItem('upstate');
  324. this.files=this.files.filter(fileObj=>{
  325. if( fileObj.id == fileId){
  326. console.log(fileObj);
  327. this.uploader.removeFile(fileId);
  328. this.namelist.splice(this.namelist.findIndex(item => item === fileObj.name), 1)
  329. }
  330. return fileObj.id != fileId
  331. })
  332. }
  333. // }
  334. },
  335. },
  336. datalish(){
  337. }
  338. }
  339. </script>
  340. <style>
  341. .webuploader-container {
  342. position: relative;
  343. }
  344. .webuploader-pick{
  345. color: #fff !important;
  346. }
  347. .webuploader-element-invisible {
  348. position: absolute !important;
  349. clip: rect(1px 1px 1px 1px); /* IE6, IE7 */
  350. clip: rect(1px,1px,1px,1px);
  351. }
  352. .webuploader-pick {
  353. position: relative;
  354. display: inline-block;
  355. cursor: pointer;
  356. padding: 1px 2px;
  357. font-size: 12px;
  358. color: #606266;
  359. text-align: center;
  360. border-radius: 3px;
  361. overflow: hidden;
  362. }
  363. .webuploader-pick-hover {
  364. background: #00a2d4;
  365. }
  366. .webuploader-pick-disable {
  367. opacity: 0.6;
  368. pointer-events:none;
  369. }
  370. .disflex{
  371. display: flex;
  372. align-items: center;
  373. }
  374. .text_color .webuploader-pick{
  375. color: #d5d5d5;
  376. }
  377. .el-progress__text{
  378. color: #d5d5d5 !important;
  379. }
  380. </style>