const pickerOpts = {
types: [
{
description: "Images",
accept: {
"image/*": [".png", ".gif", ".jpeg", ".jpg"],
},
},
],
excludeAcceptAllOption: true,
multiple: false,
};
document.getElementById('file-input').addEventListener('click', async () => {
try {
const [fileHandle] = await window.showOpenFilePicker(pickerOpts);
const file = await fileHandle.getFile();
console.log('选择的文件名称:', file.name);
// 在这里可以对选择的文件进行进一步的处理
} catch (error) {
console.error('文件选择错误:', error);
}
});
参考:https://mdn.org.cn/en-US/docs/Web/API/Window/showOpenFilePicker#browser_compatibility
标签:文件,const,选择,html,file,error,input From: https://www.cnblogs.com/atq999/p/18456446