index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. window.onload = function() {
  2. const Bucket = 'examplebucket-1250000000'; /* 存储桶 */
  3. const Region = 'ap-beijing'; /* 存储桶所在地域,必须字段 */
  4. // SecretId 和 SecretKey请登录 https://console.cloud.tencent.com/cam/capi 进行查看和管理
  5. const cos = new COS({
  6. SecretId: '',
  7. SecretKey: '',
  8. });
  9. /*
  10. 实现了以下功能
  11. - 文件列表
  12. - 上传文件
  13. - 上传文件夹
  14. - 下载文件
  15. - 删除文件
  16. */
  17. const vm = new Vue({
  18. el: '#app',
  19. data() {
  20. return {
  21. columns: [
  22. { label: '名称', value: 'Key' },
  23. { label: '大小', value: 'Size' },
  24. { label: '修改时间', value: 'LastModified' },
  25. { label: '操作', value: 'action' }
  26. ],
  27. list: [],
  28. Prefix: '',
  29. Marker: '',
  30. hasMore: false,
  31. }
  32. },
  33. computed: {
  34. // 面包屑导航条
  35. navList() {
  36. const prefixes = this.Prefix.split('/').filter(Boolean);
  37. const folders = prefixes.map((item, index) => {
  38. return {
  39. name: item,
  40. Prefix: prefixes.slice(0, index + 1).join('/') + '/',
  41. };
  42. });
  43. return [{ name: Bucket, Prefix: ''}].concat(folders);
  44. },
  45. },
  46. created() {
  47. this.getFileList();
  48. },
  49. methods: {
  50. // 查询文件列表
  51. getFileList(loadMore) {
  52. const { Prefix, Marker } = this;
  53. cos.getBucket({
  54. Bucket, /* 必须 */
  55. Region, /* 存储桶所在地域,必须字段 */
  56. Prefix, /* 非必须 */
  57. Marker, /* 非必须 */
  58. Delimiter: '/', /* 非必须 */
  59. }, (err, data) => {
  60. if(err) {
  61. console.log(err);
  62. return;
  63. }
  64. const folder = data.CommonPrefixes.map((item) => {
  65. return {
  66. Prefix: item.Prefix,
  67. name: item.Prefix.replace(Prefix, '').slice(0,-1),
  68. isDir: true,
  69. }
  70. });
  71. const files = data.Contents.filter((item) => !item.Key.endsWith('/'))
  72. .map((item) => {
  73. return {
  74. ...item,
  75. name: item.Key.replace(Prefix, ''),
  76. }
  77. });
  78. const list = folder.concat(files);
  79. this.hasMore = data.IsTruncated;
  80. this.Marker = data.NextMarker || '';
  81. if (loadMore) {
  82. this.list = [...this.list, ...list];
  83. } else {
  84. this.list = list;
  85. }
  86. });
  87. },
  88. // 点击面包屑
  89. navClick(item) {
  90. this.openFolder(item.Prefix);
  91. },
  92. // 打开文件夹
  93. openFolder(prefix) {
  94. this.Prefix = prefix;
  95. this.hasMore = false;
  96. this.Marker = '';
  97. this.getFileList();
  98. },
  99. // 上传文件
  100. uploadFileClick() {
  101. document.querySelectorAll('.file-select')[0].click();
  102. },
  103. // 上传文件夹
  104. uploadFolderClick() {
  105. document.querySelectorAll('.folder-select')[0].click();
  106. },
  107. // 上传
  108. uploadChange(events) {
  109. const files = events.currentTarget.files;
  110. const uploadFileList = [...files].map((file) => {
  111. const path = file.webkitRelativePath || file.name;
  112. return {
  113. Bucket,
  114. Region,
  115. Key: this.Prefix + path,
  116. Body: file,
  117. }
  118. });
  119. cos.uploadFiles({
  120. files: uploadFileList,
  121. SliceSize: 1024 * 1024 * 10, /* 设置大于10MB采用分块上传 */
  122. onProgress: function (info) {
  123. var percent = parseInt(info.percent * 10000) / 100;
  124. var speed = parseInt(info.speed / 1024 / 1024 * 100) / 100;
  125. console.log('进度:' + percent + '%; 速度:' + speed + 'Mb/s;');
  126. },
  127. onFileFinish: function (err, data, options) {
  128. console.log(options.Key + '上传' + (err ? '失败' : '完成'));
  129. },
  130. }, (err, data) => {
  131. if (err) {
  132. console.log('上传失败', err);
  133. return;
  134. }
  135. // 刷新列表前初始化
  136. this.hasMore = false;
  137. this.Marker = '';
  138. this.getFileList();
  139. });
  140. },
  141. // 加载更多
  142. loadMore() {
  143. this.getFileList(true);
  144. },
  145. // 下载
  146. downloadFile(file) {
  147. cos.getObjectUrl({
  148. Bucket, /* 必须 */
  149. Region, /* 存储桶所在地域,必须字段 */
  150. Key: file.Key, /* 必须 */
  151. }, function(err, data) {
  152. if (err) {
  153. console.log(err);
  154. return;
  155. }
  156. const url = data.Url + (data.Url.indexOf('?') > -1 ? '&' : '?') + 'response-content-disposition=attachment'; // 补充强制下载的参数
  157. // 使用iframe下载
  158. const elemIF = document.createElement("iframe");
  159. elemIF.src = url;
  160. elemIF.style.display = "none";
  161. document.body.appendChild(elemIF);
  162. });
  163. },
  164. // 删除
  165. deleteFile(file) {
  166. cos.deleteObject({
  167. Bucket, /* 必须 */
  168. Region, /* 存储桶所在地域,必须字段 */
  169. Key: file.Key /* 必须 */
  170. }, (err, data) => {
  171. if (err) {
  172. console.log(err);
  173. return;
  174. }
  175. // 刷新列表前初始化
  176. this.hasMore = false;
  177. this.Marker = '';
  178. this.getFileList();
  179. });
  180. },
  181. },
  182. });
  183. }