中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
文件上传的实现。
微信扫码分享
import { BusinessError, request } from '@kit.BasicServicesKit'; let uploadTask: request.UploadTask; let uploadConfig: request.UploadConfig = { url: 'http://www.mywebsite.com', //替换真实的地址 header: { 'Accept': '*/*' }, method: "POST", files: [{ filename: "test", name: "test", uri: "internal://cache/test.jpg", type: "jpg" }], data: [{ name: "name123", value: "123" }], }; try { request.uploadFile(getContext(), uploadConfig).then((data: request.UploadTask) => { uploadTask = data; uploadTask.on("progress", (uploadedSize: number, totalSize: number) => { //监听上传进度 }) uploadTask.on("complete", (taskStates) => { //上传完毕 }) //。。。。。其他状态的监听 }).catch((err: BusinessError) => { console.error(`Failed to request the upload. Code: ${err.code}, message: ${err.message}`); }); } catch (err) { console.error(`Failed to request the upload. err: ${JSON.stringify(err)}`); }