Reactjs 只允许使用React.js上载一种类型的文件
我在我的应用程序中使用了Ant Design的上传程序Reactjs 只允许使用React.js上载一种类型的文件,reactjs,antd,Reactjs,Antd,我在我的应用程序中使用了Ant Design的上传程序 function beforeUpload(file) { const filesFormats = [".doc", ".docx", "application/pdf"]; console.log(file); const isRightFormat = filesFormats.includes(file.type); console.log(isRightF
function beforeUpload(file) {
const filesFormats = [".doc", ".docx", "application/pdf"];
console.log(file);
const isRightFormat = filesFormats.includes(file.type);
console.log(isRightFormat);
if (!isRightFormat) {
message.error("You can only upload pdf and doc files");
return;
}
const isLt2M = file.size / 1024 / 1024 < 2;
if (!isLt2M) {
message.error("Image must smaller than 2MB!");
}
return isRightFormat && isLt2M;
}
class Avatar extends React.Component {
state = {
loading: false
};
handleChange = (info) => {
console.log("info, ", info);
if (info.file.status === "uploading") {
this.setState({ loading: true });
return;
}
if (info.file.status === "done") {
// Get this url from response in real world.
this.setState({ loading: false });
}
};
render() {
const { loading } = this.state;
const uploadButton = (
<div>
{loading ? <LoadingOutlined /> : <PlusOutlined />}
<div style={{ marginTop: 8 }}>Upload</div>
</div>
);
return (
<Upload
name="avatar"
listType="picture-card"
className="avatar-uploader"
showUploadList={true}
action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
beforeUpload={beforeUpload}
onChange={this.handleChange}
>
{uploadButton}
</Upload>
);
}
}
代码可以工作,但当我上传图像时,它仍然作为列表项出现在uploader图标中,但这不可能,因为我设置了限制
为什么会发生这种情况,以及如何实现我上面描述的目标
您需要为您的
上传组件使用accept
属性。已指定格式
比如说,
<Upload accept='.doc,.docx,application/pdf'>
Text
</Upload>
正文
还可以查看文档以了解更多信息:您需要为您的上传组件使用accept
属性。已指定格式
比如说,
<Upload accept='.doc,.docx,application/pdf'>
Text
</Upload>
正文
还可以查看文档了解更多信息:回答得不错,还有一个问题。如果你能帮忙,那就太好了。我忘了在问题中添加,但是,您知道下一个问题的解决方案吗:例如,现在,如果您上载图像并将鼠标悬停在图标上,则会出现一个带有删除选项和预览选项的图标。如何对我将上载的这些文档进行预览?例如pdf。感谢您使用onPreview
属性,然后您可以像这样嵌入PDF:即使我添加了onPreview
,也会禁用PDF的预览。你能告诉我怎么做吗?我对这个库不太熟悉。试着在GitHub上询问维护人员,或者在这里问一个新问题。回答不错,还有一个问题。如果你能帮忙,那就太好了。我忘了在问题中添加,但是,您知道下一个问题的解决方案吗:例如,现在,如果您上载图像并将鼠标悬停在图标上,则会出现一个带有删除选项和预览选项的图标。如何对我将上载的这些文档进行预览?例如pdf。感谢您使用onPreview
属性,然后您可以像这样嵌入PDF:即使我添加了onPreview
,也会禁用PDF的预览。你能告诉我怎么做吗?我对这个库不太熟悉。试着在GitHub上询问维护人员,或者在这里问一个新问题。