{"id":137,"date":"2021-12-07T11:59:20","date_gmt":"2021-12-07T03:59:20","guid":{"rendered":"https:\/\/yichenghui.net\/?p=137"},"modified":"2021-12-07T11:59:20","modified_gmt":"2021-12-07T03:59:20","slug":"%e5%89%8d%e7%ab%af%e5%88%86%e7%89%87%e4%b8%8a%e4%bc%a0%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/yichenghui.net\/index.php\/2021\/12\/07\/%e5%89%8d%e7%ab%af%e5%88%86%e7%89%87%e4%b8%8a%e4%bc%a0%e6%96%87%e4%bb%b6\/","title":{"rendered":"\u524d\u7aef\u5206\u7247\u4e0a\u4f20\u6587\u4ef6"},"content":{"rendered":"\n<pre class=\"wp-block-preformatted\">\/\/\u83b7\u53d6\u6570\u636e\u7c7b\u578b\r\nconst GetDataType = any => {\r\n    let str = Object.prototype.toString.call(any);\r\n    return str.slice(8, -1);\r\n}\r\n\r\n\/\/uploadFile\u65b9\u6cd5\u57fa\u7840\u914d\u7f6e\r\nconst uploadBaseConfig = {\r\n    file: File,\r\n    maxSize: 4 * 1024 * 1024,\r\n    taskLength: 1,\r\n}\r\n\r\n\/\/\u6587\u4ef6\u4e0a\u4f20\u65b9\u6cd5\nexport async function uploadFile(config = uploadBaseConfig) {\r\n    return new Promise(resolve => {\r\n        let taskResolve = []; \/\/\u6240\u6709\u8bf7\u6c42\u7684\u76f8\u5e94\r\n\r\n        if (GetDataType(config) === \"File\") {\r\n            config = {\r\n                ...uploadBaseConfig,\r\n                file: arguments[0],\r\n                ...arguments[1],\r\n            };\r\n        }\r\n        const { file, maxSize, taskLength } = config; \/\/uploadFile \u8bf7\u6c42\u914d\u7f6e\u5bf9\u8c61\r\n        if (GetDataType(file) !== \"File\") {\r\n            return Promise.reject(new Error(\"\u6587\u4ef6\u4e0d\u80fd\u4e3a\u7a7a\"));\r\n        };\r\n\r\n        \/\/\u8bf7\u6c42\u65b9\u6cd5\r\n        const uploadMethod = formData => axios({\r\n            method: \"post\",\r\n            url: `\/index\/util\/upload`,\r\n            data: formData,\r\n            headers: {\r\n                \"Content-Type\": \"multipart\/form-data\"\r\n            },\r\n        });\r\n\r\n        \/\/ \u5c0f\u4e8e4Mb\u76f4\u63a5\u4f20\r\n        if (file.size &lt;= maxSize) {\r\n            const form = new FormData();\r\n            form.append(\"file\", file);\r\n            uploadMethod(form).then(res => {\r\n                resolve(res);\r\n            });\r\n            return\r\n        }\r\n\r\n        \/\/Symbol\u517c\u5bb9\u5904\u7406\r\n        const makeIterator = arr => {\r\n            if (\/native code\/.test(Symbol.toString())) {\r\n                return arr[Symbol.iterator]();\r\n            }\r\n            let nextIndex = 0;\r\n            return {\r\n                next() {\r\n                    if (nextIndex &lt; arr.length) {\r\n                        return { value: arr[nextIndex++], done: false }\r\n                    }\r\n                    return { value: undefined, done: true }\r\n                }\r\n            }\r\n        };\r\n\r\n        \/\/\u8bf7\u6c42\u961f\u5217\r\n        const task = arr => {\r\n            const { value } = arr.next();\r\n            if (value) {\r\n                Promise.all(value.map(item => uploadMethod(item))).then(result => {\r\n                    taskResolve = taskResolve.concat(result);\r\n                    setTimeout(() => task(arr), 1000 * 5);\r\n                });\r\n            } else {\r\n                resolve(taskResolve)\r\n            }\r\n        };\r\n\r\n        const length = Math.ceil(file.size \/ maxSize); \/\/\u6587\u4ef6\u5206\u7247\u957f\u5ea6 \r\n        const taskArrLength = Math.ceil(length \/ taskLength);\/\/promise\u4efb\u52a1\u961f\u5217\u957f\u5ea6\r\n        \/\/\u5207\u5272\u6587\u4ef6\u5bf9\u8c61\r\n        const arr = [...new Array(length)].map((item, index) => {\r\n            const start = index * maxSize;\r\n            const end = start + maxSize;\r\n            const fileContent = new File(\/\/ \u6587\u4ef6\u5185\u5bb9\r\n                [file.slice(start, end)],\r\n                file.name,\r\n                { type: file.type }\r\n            );\r\n            const form = new FormData();\r\n            form.append(\"file\", fileContent);\r\n\r\n            return form;\r\n        });\r\n\r\n        \/\/ \u961f\u5217\u6570\u7ec4\r\n        const taskArr = [... new Array(taskArrLength)].map((item, index) => {\r\n            const start = index * taskLength;\r\n            const end = start + taskLength;\r\n            return arr.slice(start, end);\r\n        });\r\n        const tasklist = makeIterator(taskArr); \/\/\u751f\u6210\u4efb\u52a1\u961f\u5217\r\n        task(tasklist);\r\n    })\r\n\r\n};<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/\/\u83b7\u53d6\u6570\u636e\u7c7b\u578b const GetDataType = any => { let str = Object. &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/yichenghui.net\/index.php\/2021\/12\/07\/%e5%89%8d%e7%ab%af%e5%88%86%e7%89%87%e4%b8%8a%e4%bc%a0%e6%96%87%e4%bb%b6\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u524d\u7aef\u5206\u7247\u4e0a\u4f20\u6587\u4ef6\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kadence_starter_templates_imported_post":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-137","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/yichenghui.net\/index.php\/wp-json\/wp\/v2\/posts\/137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yichenghui.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yichenghui.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yichenghui.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yichenghui.net\/index.php\/wp-json\/wp\/v2\/comments?post=137"}],"version-history":[{"count":1,"href":"https:\/\/yichenghui.net\/index.php\/wp-json\/wp\/v2\/posts\/137\/revisions"}],"predecessor-version":[{"id":138,"href":"https:\/\/yichenghui.net\/index.php\/wp-json\/wp\/v2\/posts\/137\/revisions\/138"}],"wp:attachment":[{"href":"https:\/\/yichenghui.net\/index.php\/wp-json\/wp\/v2\/media?parent=137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yichenghui.net\/index.php\/wp-json\/wp\/v2\/categories?post=137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yichenghui.net\/index.php\/wp-json\/wp\/v2\/tags?post=137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}