comp-upload.component.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
  2. import { NzUploadModule } from 'ng-zorro-antd/upload';
  3. import { CommonCompModule } from '../../services/common.modules';
  4. import { NzMessageService } from 'ng-zorro-antd/message';
  5. import { NzUploadChangeParam, NzUploadFile } from 'ng-zorro-antd/upload';
  6. import { ProvierOssAli } from './provider-oss-aliyun';
  7. import { NzImageService } from 'ng-zorro-antd/image';
  8. import { NzImageModule } from 'ng-zorro-antd/image';
  9. import { NzNotificationModule } from 'ng-zorro-antd/notification';
  10. import { NzNotificationService } from 'ng-zorro-antd/notification';
  11. @Component({
  12. selector: 'app-comp-upload',
  13. standalone: true,
  14. imports: [NzUploadModule, CommonCompModule, NzImageModule,NzNotificationModule],
  15. templateUrl: './comp-upload.component.html',
  16. styleUrls: ['./comp-upload.component.scss'],
  17. })
  18. export class CompUploadComponent implements OnInit {
  19. @Input('title') title: string = '上传文件';
  20. @Input('files') files: Array<any> = [];
  21. @Output() change: EventEmitter<any> = new EventEmitter<any>();
  22. @Input('width') width: number = 0;
  23. @Input('maxlenght') maxlenght: number = 1; //文件数量限制
  24. @Input('type') type: string = 'file';
  25. @Input('size') size: number = 2048; //上传文件限制大小单位KB
  26. @Input('acl') acl: "public-read-write" | "public-read" | "private" | "default" = "default"
  27. get accept() {
  28. let type;
  29. switch (this.type) {
  30. case 'image':
  31. type = 'image/*';
  32. break;
  33. case 'pdf':
  34. type = 'application/pdf';
  35. break;
  36. case 'audio':
  37. type = 'audio/*';
  38. break;
  39. case 'video':
  40. type = 'video/*';
  41. break;
  42. default:
  43. type = 'file';
  44. break;
  45. }
  46. return type;
  47. }
  48. fileList: any = [
  49. // {
  50. // name: 'xxx.png',
  51. // status: 'done',
  52. // url: 'http://www.baidu.com/xxx.png'
  53. // },
  54. ];
  55. ossFileList: any;
  56. Previewfilelist: any;
  57. ossProvider: { upload: any, signatureUrl: any, download:any } | undefined;
  58. disabled: boolean = false
  59. constructor(
  60. private msg: NzMessageService,
  61. private nzImageService: NzImageService,
  62. private notification: NzNotificationService
  63. ) {
  64. this.ossProvider = new ProvierOssAli();
  65. }
  66. ngOnInit() {
  67. this.fileList = this.files.map((item: any) => {
  68. console.log(item);
  69. return {
  70. url: item?.url,
  71. name: item?.name,
  72. status: 'done',
  73. };
  74. });
  75. this.Previewfilelist = this.fileList;
  76. }
  77. async handleChange(info: NzUploadChangeParam) {
  78. console.log(info);
  79. if (info.type == 'removed') {
  80. return
  81. }
  82. let isEncrypt = await this.isEncrypt(info)
  83. // if (isEncrypt) {
  84. // return
  85. // }
  86. this.disabled = true
  87. if (info.file.status !== 'uploading') {
  88. // 选择文件后,自动开始上传
  89. // console.log(info.file, info.fileList);
  90. let ossFileList = await this.uploadAllFileList(info?.fileList);
  91. let nowUploadFiles = ossFileList?.map((item) => {
  92. let f = {
  93. name: item.locaname,
  94. url: item?.url,
  95. };
  96. return f;
  97. })
  98. let fileList = [...this.fileList, ...nowUploadFiles];
  99. this.fileList = fileList.slice(-this.maxlenght);
  100. // fileList = fileList.map((file) => {
  101. // if (file.response) {
  102. // file.url = file.response.url;
  103. // }
  104. // return file;
  105. // });
  106. // this.fileList = fileList
  107. this.Previewfilelist = [...this.fileList];
  108. // this.ossFileList = ossFileList;
  109. this.change.emit(this.fileList);
  110. this.disabled = false
  111. }
  112. if (info.file.status === 'done') {
  113. this.msg.success(`${info.file.name} file uploaded successfully`);
  114. console.log(info);
  115. } else if (info.file.status === 'error') {
  116. // this.msg.error(`${info.file.name} file upload failed.`);
  117. }
  118. // this.change.emit(info.fileList)
  119. }
  120. async uploadAllFileList(fileList: Array<NzUploadFile>) {
  121. let ossFileList = [];
  122. for (let index = 0; index < fileList.length; index++) {
  123. let file = fileList[index];
  124. if (file) {
  125. let ossFile;
  126. try {
  127. ossFile = await this.ossProvider?.upload(file?.originFileObj,null,{acl:this.acl});
  128. } catch (err) { }
  129. if (ossFile) {
  130. ossFile.locaname = file.name;
  131. ossFileList.push(ossFile);
  132. }
  133. }
  134. }
  135. return ossFileList;
  136. }
  137. onRemove = (e: any): boolean => {
  138. console.log(e);
  139. let i = this.fileList.findIndex((item: any) => item.url == e?.url)
  140. this.fileList.splice(i, 1)
  141. this.change.emit(this.fileList);
  142. return false
  143. }
  144. /**预览图片 */
  145. preview = async (e: any): Promise<void> => {
  146. // let index = this.Previewfilelist.findIndex(
  147. // (item: any) => item.url == e.url
  148. // );
  149. // let file = this.Previewfilelist[index]?.url;
  150. let url = e?.url
  151. if (!/\.(jpg|jpeg|png|GIF|JPG|PNG)$/.test(url)) {
  152. // if(this.acl == 'private'){
  153. // url = await this.ossProvider?.signatureUrl(url)
  154. // }
  155. window.open(url);
  156. } else {
  157. this.nzImageService.preview([{ src: url }], { nzZoom: 1, nzRotate: 0 });
  158. }
  159. };
  160. timeOut: any
  161. /**判断pdf文件是否加密 */
  162. isEncrypt(info: NzUploadChangeParam): Promise<boolean> {//只查file,不查fileList
  163. return new Promise((resolve) => {
  164. let isEncrypt: boolean = false
  165. if (info.file.originFileObj) {
  166. let reader = new FileReader()
  167. let file = info.file.originFileObj
  168. reader.readAsArrayBuffer(file)
  169. reader.onload = () => {
  170. if (reader.result) {
  171. let files = new Blob([reader.result], { type: 'application/pdf' })
  172. files.text().then(x => {
  173. isEncrypt = x.substring(x.lastIndexOf("<<"), x.lastIndexOf(">>")).includes("/Encrypt")
  174. if (isEncrypt) {
  175. clearTimeout(this.timeOut)
  176. this.timeOut = setTimeout(() => {
  177. this.notification.create(
  178. 'error',
  179. '请勿上传加密文件',
  180. `${file.name} 为加密文件,影响导出,请替换。`,
  181. { nzDuration: 0 }
  182. );
  183. }, 1000);
  184. }
  185. resolve(isEncrypt)
  186. })
  187. } else { resolve(false) }
  188. }
  189. } else { resolve(false) }
  190. })
  191. }
  192. }