取不到数据是官方("element-plus": "^2.3.4") beforeData在beforeUploadPromise 之前导致异步设置:data 不生效文件传不上去的原因,改为http-request去处理就可以了。挺奇怪,之前用("element-plus": "^2.2.17")是没有问题的。
在before-upload里请求后端接口获取到accessid、policy、signature、dir等。
在http-request里
const httpRequest: UploadRequestHandler = async (options: UploadRequestOptions) => {console.log(options);let formdata = new FormData();formdata.append("ossaccessKeyId", dataOss.value.ossaccessKeyId);formdata.append("policy", dataOss.value.policy);formdata.append("signature", dataOss.value.signature);formdata.append("host", dataOss.value.host);formdata.append("key", dataOss.value.key);formdata.append("success_action_status", "200");formdata.append("file", options.file);uploadFileToOSS(formdata, options.action).then(res => {console.log(res);if (res.status == 200) {options.onSuccess(res);} else {ElMessage({ message: "上传失败", type: "error", showClose: true });}});
};const uploadFileToOSS = (data: any, action: string) => {return axios({url: action,method: "POST",data: data,headers: {"Content-Type": "multipart/form-data"}});
};