beUpload.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <template>
  2. <button class="info_btn" @click="addImg($event)">
  3. {{ navName }}
  4. <input
  5. type="file"
  6. :accept="acc"
  7. style="display: none"
  8. capture="camera"
  9. @change="beforeUpload($event)"
  10. />
  11. </button>
  12. </template>
  13. <script>
  14. // import "../common/aws-sdk-2.235.1.min.js";
  15. import { getNowDate } from "../../tool/Date.js";
  16. export default {
  17. components: {},
  18. props: [ 'navName','accept','progress'],
  19. data() {
  20. return {
  21. inputShow: true,
  22. acc:"",
  23. };
  24. },
  25. watch: {
  26. accept: {
  27. immediate: true,
  28. deep: true,
  29. handler(newValue, oldValue) {
  30. this.getAccept();
  31. },
  32. },
  33. },
  34. methods: {
  35. addImg(e) {
  36. console.log(e);
  37. var el = e.currentTarget || e;
  38. console.log(el);
  39. el.getElementsByTagName("input")[0].click();
  40. e.target.value = "";
  41. },
  42. beforeUpload(event) {
  43. console.log(event);
  44. // const loading = this.openLoading();
  45. var file = event.target.files[0];
  46. var credentials = {
  47. accessKeyId: "AKIATLPEDU37QV5CHLMH",
  48. secretAccessKey: "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",
  49. }; //秘钥形式的登录上传
  50. window.AWS.config.update(credentials);
  51. window.AWS.config.region = "cn-northwest-1"; //设置区域
  52. var bucket = new window.AWS.S3({
  53. params: {
  54. Bucket: "ccrb",
  55. },
  56. }); //选择桶
  57. var _this = this;
  58. _this.progress?_this.progress.show = true:'';
  59. _this.progress?_this.progress.value = 0:'';
  60. if (file) {
  61. var params = {
  62. Key:
  63. file.name.split(".")[0] +
  64. new Date().getTime() +
  65. "." +
  66. file.name.split(".")[file.name.split(".").length - 1],
  67. ContentType: file.type,
  68. Body: file,
  69. "Access-Control-Allow-Credentials": "*",
  70. ACL: "public-read",
  71. }; //key可以设置为桶的相抵路径,Body为文件, ACL最好要设置
  72. var options = {
  73. partSize: 2048 * 1024 * 1024,
  74. queueSize: 2,
  75. leavePartsOnError: true,
  76. };
  77. bucket
  78. .upload(params, options)
  79. .on("httpUploadProgress", function (evt) {
  80. //进度条
  81. _this.progress?_this.progress.value = Math.round((evt.loaded/evt.total)*100):'';
  82. })
  83. .send(function (err, data) {
  84. _this.inputShow = true;
  85. if (err) {
  86. _this.$message.error("上传失败");
  87. } else {
  88. let size = file.size;
  89. let unit = "B"
  90. if(size>1000000000){
  91. size = size/1024/1024/1024;
  92. unit = "G"
  93. }else if(size>1000000){
  94. size = size/1024/1024
  95. unit = "MB"
  96. }else if(size>1000){
  97. size = size/1024
  98. unit = "KB"
  99. }
  100. size = Math.floor(size);
  101. _this.$emit('getFile',{fileName:file.name,size:`${size}${unit}`,uploadTime:getNowDate(),url:data.Location})
  102. }
  103. });
  104. }
  105. },
  106. getAccept(){
  107. if(this.accept){
  108. this.acc = this.accept;
  109. }else{
  110. this.acc = "*";
  111. }
  112. },
  113. },
  114. created(){
  115. this.getAccept();
  116. },
  117. };
  118. </script>
  119. <style scoped>
  120. .info_btn{
  121. color: #fff;
  122. background-color: #0f7eff;
  123. padding: 8px 24px;
  124. font-size: 0.9375rem;
  125. box-shadow: 0px 1px 3px 0px rgb(0 0 0 / 20%), 0px 2px 2px 0px rgb(0 0 0 / 14%),
  126. 0px 3px 1px -2px rgb(0 0 0 / 12%);
  127. min-width: 64px;
  128. font-weight: 500;
  129. border-radius: 4px;
  130. box-sizing: border-box;
  131. border: none;
  132. cursor: pointer;
  133. }
  134. .info_btn:hover {
  135. background-color: #4f7cd5 !important;
  136. }
  137. </style>