本以为使用const isCSV = file.type === 'text/csv';进行判断即可。谁知在某些环境下居然无效。查阅后发现,csv文件的MIME 类型居然不是唯一的,再添加 'application/vnd.ms-excel' 即可。
完整代码如下:
const uploadProps: UploadProps = {
accept: ".csv",
name: "file",
action: `${UPLOAD_CSV_URL}`,
headers: {},
onChange: (info) => {
if (info.file.status === "done" && info.file.response.code === 0) {
// some code
} else if (info.file.status === "error") {
console.error(`${info.file.name} file upload failed.`);
}
},
onRemove: () => {
// some code
},
beforeUpload: (file) => {
const isCSV = ["text/csv", "application/vnd.ms-excel"].includes(file.type);
if (!isCSV) {
console.log("file.type", file.type);
message.error(`所选文件不是csv格式!`);
}
return isCSV || Upload.LIST_IGNORE;
},
maxCount: 1,
};