瀏覽代碼

文件上传

zhaoxiaohai 2 年之前
父節點
當前提交
eaf34f94ba
共有 1 個文件被更改,包括 14 次插入29 次删除
  1. 14 29
      components/My_upload/index.js

+ 14 - 29
components/My_upload/index.js

@@ -13,6 +13,15 @@ Component({
         accept: {
             type: String,
             value: "all"
+        },
+        /* 上传回调 */
+        uploadCallback: {
+            type: Function
+        },
+        /* 文件夹ID */
+        parentid: {
+            type: String,
+            value: wx.getStorageSync('siteP').appfolderid
         }
     },
 
@@ -33,6 +42,7 @@ Component({
                 // 初始化数据
                 let that = this,
                     data = this.requestType(event.detail.file[i]);
+                data.content.filename = data.content.filename ? data.content.filename : `${Date.now()}.${data.content.filetype}`;
                 //发送请求
                 wx.getFileSystemManager().readFile({
                     filePath: event.detail.file[i].url,
@@ -40,7 +50,6 @@ Component({
                         //返回临时文件路径
                         const fileData = result.data;
                         _Http.basic(data).then(res => {
-                            console.log("res", res)
                             if (res.msg == "成功") {
                                 that.uploadFile(res.data, fileData)
                             } else {
@@ -67,14 +76,13 @@ Component({
                 "content": {
                     "filename": file.name,
                     "filetype": ext,
-                    "parentid": 2
+                    "parentid": this.data.parentid
                 }
             }
         },
         /* 上传成功反馈 */
         uploadFile(res, data) {
-            console.log('asd', res, data)
-            var that = this
+            var that = this;
             wx.request({
                 url: res.uploadurl,
                 method: "PUT",
@@ -83,7 +91,6 @@ Component({
                     'content-type': 'application/octet-stream'
                 },
                 success(a) {
-                    console.log("a", a)
                     _Http.basic({
                         "classname": "system.attachment.huawei.OBS",
                         "method": "uploadSuccess",
@@ -91,30 +98,8 @@ Component({
                             "serialfilename": res.serialfilename
                         }
                     }).then(s => {
-                        return console.log(s);
-                        if (res.msg != "成功") return;
-                        let fileList = that.data.fileList;
-                        for (let i = 0; i < res.data.length; i++) {
-                            let arr = {
-                                url: res.data[i].fobsurl,
-                                ownerid: res.data[i].ownerid,
-                                tattachmentid: res.data[i].tattachmentid,
-                                ownertable: res.data[i].ownertable,
-                                fdocument: res.data[i].fdocument,
-                            }
-                            fileList.push(arr)
-                        };
-                        // 用户头像 先删除 在修改
-                        if (that.data.upType == "userImage" && fileList.length >= 2) {
-                            that.dleeteDealWith(0);
-                        }
-                        that.setData({
-                            fileList
-                        });
-                        /* 返回数据 */
-                        that.triggerEvent("imageChange", {
-                            fileList
-                        })
+                        if (s.msg != "成功") return;
+                        that.triggerEvent("uploadCallback", s.data.attachmentids);
                     }).catch(err => {
                         console.log(err)
                     })