beUpload.vue 3.5 KB

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