beUpload.vue 3.0 KB

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