Explorar o código

支持H5上传

xiaohaizhao %!s(int64=2) %!d(string=hai) anos
pai
achega
6a564bac9c
Modificáronse 1 ficheiros con 53 adicións e 23 borrados
  1. 53 23
      components/my-upload.vue

+ 53 - 23
components/my-upload.vue

@@ -16,8 +16,8 @@ export default {
             default: "99"
         },
         parentid: { //上传文件夹ID
-            type: String,
-            value: wx.getStorageSync('siteP').appfolderid
+            type: [String, Number],
+            default: uni.getStorageSync('siteP').appfolderid
         },
         uploadCallback: { //上传回调
             type: Function
@@ -27,49 +27,76 @@ export default {
         afterRead({ file }) {
             const that = this;
             file.forEach(v => {
+                // #ifdef H5
+                this.getArrayBuffer(v).then(data => that.handleUploadFile(this.requestType(v), data))
+                // #endif
+                // #ifndef H5
                 uni.getFileSystemManager().readFile({
                     filePath: v.url,
-                    success: (data) => {
-                        that.$Http.basic(this.requestType(v)).then(res => {
-                            if (res.msg == "成功") {
-                                that.uploadFile(res.data, data)
-                            } else {
-                                uni.showToast({
-                                    title: `${data.filename}.${data.serialfilename}`,
-                                    icon: "none"
-                                })
-                            }
-                        })
-                    },
+                    success: data => that.handleUploadFile(this.requestType(v), data.data),
                     fail: console.error
                 })
+                // #endif
             });
-
         },
         /* 请求类型 */
         requestType(file) {
-            //获取文件后缀
-            var index = file.url.lastIndexOf(".");
-            var ext = file.url.substr(index + 1);
+            // #ifdef H5
+            var ext = file.type.split("/")[1] || file.name.substr(file.name.lastIndexOf(".") + 1);
+            // #endif
+            // #ifndef H5
+            var ext = file.type.split("/")[1] || file.url.substr(file.url.lastIndexOf(".") + 1) || file.name.substr(file.name.lastIndexOf(".") + 1);
+            // #endif
             //文件名称
             return {
                 "classname": "system.attachment.huawei.OBS",
                 "method": "getFileName",
                 "content": {
-                    "filename": file.name || `${Date.now()}.${ext}`,
+                    "filename": `${Date.now() + file.size}.${ext}`,
                     "filetype": ext,
                     "parentid": this.parentid
                 }
             }
         },
-
+        handleUploadFile(file, data) {
+            this.$Http.basic(file).then(res => {
+                if (res.msg == "成功") {
+                    this.uploadFile(res.data, data)
+                } else {
+                    uni.showToast({
+                        title: `${data.filename}.${data.serialfilename}`,
+                        icon: "none"
+                    })
+                }
+            })
+        },
+        getArrayBuffer(file) {
+            return new Promise((resolve, reject) => {
+                let xhr = new XMLHttpRequest()
+                xhr.open('GET', file.url, true)
+                xhr.responseType = 'blob'
+                xhr.onload = function () {
+                    if (this.status == 200) {
+                        let myBlob = this.response
+                        let files = new window.File([myBlob], file.name, { type: file.url.substr(file.url.lastIndexOf(".") + 1) }) // myBlob.type 自定义文件名
+                        const reader = new FileReader();
+                        reader.readAsArrayBuffer(files);
+                        reader.onload = () => resolve(reader.result);
+                        reader.onerror = (error) => reject(error);
+                    } else {
+                        reject(false)
+                    }
+                }
+                xhr.send()
+            })
+        },
         /* 上传成功反馈 */
         uploadFile(res, data) {
             var that = this;
             uni.request({
                 url: res.uploadurl,
                 method: "PUT",
-                data: data,
+                data,
                 header: {
                     'content-type': 'application/octet-stream'
                 },
@@ -82,13 +109,16 @@ export default {
                         }
                     }).then(s => {
                         console.log("文件上传反馈", s)
-                        if (!that.cutoff(s.msg)) that.$emit("uploadCallback", s.data.attachmentids[0]);
+                        if (!that.cutoff(s.msg)) that.$emit("uploadCallback", s.data.attachmentids);
                     }).catch(err => {
                         console.error(err)
                     })
+                },
+                fail(err) {
+                    console.log(err)
                 }
             })
-        },
+        }
     },
 }
 </script>