my-upload.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <u-upload @afterRead="afterRead" :maxCount="maxCount" :accept="accept" multiple>
  3. <view v-if="showLoading">
  4. <u-loading-icon v-if="loading" />
  5. <slot v-else />
  6. </view>
  7. <slot v-else />
  8. </u-upload>
  9. </template>
  10. <script>
  11. export default {
  12. name: "MyUpload",
  13. props: {
  14. accept: { //接受的文件类型,file只支持H5(只有微信小程序才支持把accept配置为all、media)
  15. type: String,
  16. default: "image"
  17. },
  18. maxCount: {
  19. type: String,
  20. default: "99"
  21. },
  22. parentid: { //上传文件夹ID
  23. type: [String, Number],
  24. default: uni.getStorageSync('siteP').appfolderid
  25. },
  26. uploadCallback: { //上传回调
  27. type: Function
  28. },
  29. showLoading: {
  30. type: Boolean,
  31. default: true
  32. }
  33. },
  34. data() {
  35. return {
  36. loading: false
  37. }
  38. },
  39. methods: {
  40. afterRead({ file }) {
  41. const that = this;
  42. file.forEach(v => {
  43. // #ifdef H5
  44. this.getArrayBuffer(v).then(data => that.handleUploadFile(this.requestType(v), data))
  45. // #endif
  46. // #ifndef H5
  47. uni.getFileSystemManager().readFile({
  48. filePath: v.url,
  49. success: data => that.handleUploadFile(this.requestType(v), data.data),
  50. fail: console.error
  51. })
  52. // #endif
  53. });
  54. },
  55. /* 请求类型 */
  56. requestType(file) {
  57. // #ifdef H5
  58. var ext = file.name.substr(file.name.lastIndexOf(".") + 1);
  59. // #endif
  60. // #ifndef H5
  61. var ext = file.type.split("/")[1] || file.url.substr(file.url.lastIndexOf(".") + 1) || file.name.substr(file.name.lastIndexOf(".") + 1);
  62. // #endif
  63. //文件名称
  64. return {
  65. "classname": "system.attachment.huawei.OBS",
  66. "method": "getFileName",
  67. "content": {
  68. "filename": `${Date.now() + file.size}.${ext}`,
  69. "filetype": ext,
  70. "parentid": this.parentid
  71. }
  72. }
  73. },
  74. handleUploadFile(file, data) {
  75. this.loading = true;
  76. this.$Http.basic(file).then(res => {
  77. if (res.msg == "成功") {
  78. this.uploadFile(res.data, data)
  79. } else {
  80. uni.showToast({
  81. title: `${data.filename}.${data.serialfilename}`,
  82. icon: "none"
  83. })
  84. }
  85. })
  86. },
  87. getArrayBuffer(file) {
  88. return new Promise((resolve, reject) => {
  89. let xhr = new XMLHttpRequest()
  90. xhr.open('GET', file.url, true)
  91. xhr.responseType = 'blob'
  92. xhr.onload = function () {
  93. if (this.status == 200) {
  94. let myBlob = this.response
  95. let files = new window.File([myBlob], file.name, { type: file.url.substr(file.url.lastIndexOf(".") + 1) }) // myBlob.type 自定义文件名
  96. const reader = new FileReader();
  97. reader.readAsArrayBuffer(files);
  98. reader.onload = () => resolve(reader.result);
  99. reader.onerror = (error) => reject(error);
  100. } else {
  101. reject(false)
  102. }
  103. }
  104. xhr.send()
  105. })
  106. },
  107. /* 上传成功反馈 */
  108. uploadFile(res, data) {
  109. var that = this;
  110. uni.request({
  111. url: res.uploadurl,
  112. method: "PUT",
  113. data,
  114. header: {
  115. 'content-type': 'application/octet-stream'
  116. },
  117. success() {
  118. that.$Http.basic({
  119. "classname": "system.attachment.huawei.OBS",
  120. "method": "uploadSuccess",
  121. "content": {
  122. "serialfilename": res.serialfilename
  123. }
  124. }).then(s => {
  125. console.log("文件上传反馈", s)
  126. that.loading = false;
  127. if (!that.cutoff(s.msg)) that.$emit("uploadCallback", s.data.attachmentids);
  128. }).catch(err => {
  129. that.loading = false;
  130. console.error(err)
  131. })
  132. },
  133. fail(err) {
  134. that.loading = false;
  135. console.log(err)
  136. }
  137. })
  138. }
  139. },
  140. }
  141. </script>