uploadimg.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <view @click="fileUpload">
  3. <slot></slot>
  4. </view>
  5. </template>
  6. <script>
  7. import {upload} from '../../api/upload'
  8. export default {
  9. props:{
  10. // uploadSuccess: {
  11. // type: Function,
  12. // default: null
  13. // },
  14. },
  15. methods:{
  16. fileUpload(){
  17. let _this = this
  18. uni.chooseImage({
  19. count:1,
  20. success:(res)=>{
  21. // debugger
  22. console.log(res);
  23. // #ifdef MP-WEIXIN
  24. let index = res.tempFilePaths[0].lastIndexOf('.')
  25. res.name ='01'+res.tempFilePaths[0].substring(index)
  26. // #endif
  27. // #ifndef MP-WEIXIN
  28. res.name = res.tempFiles[0].name
  29. // #endif
  30. upload('avatar',res.name).then(result=>{
  31. if (result.data.code === 0) {
  32. _this.ossUpload(res, result.data.data);
  33. } else {
  34. uni.showToast({
  35. title: result.data.error || result.data.msg ,
  36. icon: 'none',
  37. duration: 2000
  38. })
  39. }
  40. })
  41. }
  42. })
  43. },
  44. ossUpload(file, ossToken) {
  45. let _this = this
  46. console.log(file,ossToken);
  47. // #ifndef MP-WEIXIN
  48. _this.bucketName = ossToken.bucket
  49. let formData = new FormData();
  50. formData.append('key', ossToken.dir +'/'+ file.name); //存储在oss的文件路径
  51. formData.append('OSSAccessKeyId', ossToken.accessid); //accessKeyId
  52. formData.append('policy', ossToken.policy); //policy
  53. formData.append('Signature', ossToken.signature); //签名
  54. formData.append('callback', ossToken.callback); //回调
  55. formData.append('success_action_status', 200); //成功后返回的操作码
  56. formData.append("file", file.tempFiles[0]);
  57. let xhr = new XMLHttpRequest();
  58. xhr.withCredentials = false;
  59. xhr.open("POST", ossToken.host);
  60. xhr.onload = () => {
  61. console.log(xhr.response); //输出结果
  62. _this.ossUploadFinished(JSON.parse(xhr.response))
  63. };
  64. xhr.send(formData);
  65. // #endif
  66. // #ifdef MP-WEIXIN
  67. wx.uploadFile({
  68. url:ossToken.host,
  69. header: {
  70. 'Content-Type': 'multipart/form-data'
  71. },
  72. filePath:file.tempFilePaths[0],
  73. name:'file',
  74. formData:{
  75. key:ossToken.dir +'/'+ file.name,
  76. OSSAccessKeyId:ossToken.accessid,
  77. policy:ossToken.policy,
  78. Signature: ossToken.signature,
  79. // success_action_status: '200',
  80. },
  81. success: function (res) {
  82. if (res.statusCode === 204) {
  83. let url=ossToken.host+'/'+ossToken.dir +'/'+ file.name
  84. _this.ossUploadFinished(url)
  85. } else {
  86. console.log('上传失败');
  87. }
  88. },
  89. fail: function (err) {
  90. console.info(err)
  91. }
  92. })
  93. // #endif
  94. },
  95. ossUploadFinished(res) {
  96. // #ifdef MP-WEIXIN
  97. let data = {url : res}
  98. this.$emit('upload',data)
  99. // this.uploadSuccess(data);
  100. // #endif
  101. // #ifndef MP-WEIXIN
  102. if (res.code === 0) {
  103. console.log(res);
  104. // if (this.uploadSuccess) {
  105. let data = {
  106. original: res.data.name,
  107. fileSize: res.data.size,
  108. url: res.data.url,
  109. fileName: res.data.filename,
  110. bucketName: this.bucketName
  111. }
  112. // this.uploadSuccess(data);
  113. this.$emit('upload',data)
  114. // }
  115. } else {
  116. uni.showToast({
  117. title: res.data.error || res.data.msg ,
  118. icon: 'none',
  119. duration: 2000
  120. })
  121. }
  122. // #endif
  123. }
  124. }
  125. }
  126. </script>