Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 只允许使用React.js上载一种类型的文件_Reactjs_Antd - Fatal编程技术网

Reactjs 只允许使用React.js上载一种类型的文件

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

我在我的应用程序中使用了Ant Design的上传程序

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上询问维护人员,或者在这里问一个新问题。