首页 > 其他分享 >react 文件选择器

react 文件选择器

时间:2023-12-28 16:33:35浏览次数:36  
标签:文件 const upload ele react input path type 选择器

export type FileSelectorType = {
  emit: boolean;
  type: "file" | "dir";
  callBack: (path: string, fileList: FileList) => void;
};

export const FileSelector = (props: { upload: FileSelectorType }) => {
  useEffect(() => {
    const input = document.createElement("input");
    input.setAttribute("id", "uploadInputBox");
    props.upload.type === "dir" &&
      input.setAttribute("webkitdirectory", "true");
    input.setAttribute("multiple", "true");
    input.setAttribute("type", "file");
    // input.setAttribute("style", "width: 0px;");
    input.addEventListener("change", (e) => {
      const target = e.target as HTMLInputElement;
      let path = "";
      if (target.files.length) {
        const ele = target.files[0];
        const relativePath = ele.webkitRelativePath.substring(
          0,
          ele.webkitRelativePath.lastIndexOf("/")
        );
        path = ele.path.substring(
          0,
          ele.path.lastIndexOf(relativePath) + relativePath.length
        );
      }

      props.upload.callBack(path, target.files);
    });
    document.getElementById("uploadInputBox").append(input);
    input.click();

    return () => {
      document.getElementById("uploadInputBox").removeChild(input);
    };
  }, [props.upload.emit]);
  return (
    <div id="uploadInputBox" style={{ width: 0, height: 0, opacity: 0 }}></div>
  );
};
// 调用:
  const [upload, setUpload] = useState<FileSelectorType>({
    emit: false,
    type: "dir",
    callBack: (path: string, fileList: FileList) => {},
  });

<FileSelector upload={upload} />

<Button
      onClick={() => {
        setUpload({
          emit: !upload.emit,
          type: "dir",
          callBack: (path: string, fileList: FileList) => {
            console.log(path);
            setBackupFolder(path);
          },
        });
      }}
    >
    choose folder
</Button>

 

标签:文件,const,upload,ele,react,input,path,type,选择器
From: https://www.cnblogs.com/laremehpe/p/17932976.html

相关文章

  • [JDK] 底层大法之替换JAR包中的class文件 [转载]
    0序背景近期跟踪一个第三方组件skywalking针对springcloudgateway的traceId丢失问题,需要对skywalking内部组件类进行跟踪,需要在其组件源码内部添加一些观察代码,做以增强。Q1:我没有thrid-party.jar的源码————故直接在源码上修改,然后打包(此法行不通)Q2:反编译......
  • python 将文件移入回收站
     python如果要删除一个文件,通常使用os.remove(filename)但是这样就直接从磁盘删除了。有些文件需要删除到回收站,以便误删后还能找回文件fromwin32com.shellimportshell,shellcondebug=Falsedefdeltorecyclebin(filename):print('deltorecyclebin',filename)......
  • python 文件读写权限 PermissionError: [Errno 13] Permission denied
    概述os.chmod()方法用于更改文件或目录的权限。语法chmod()方法语法格式如下:os.chmod(path,mode)参数path --文件名路径或目录路径。flags --可用以下选项按位或操作生成,目录的读权限表示可以获取目录里文件名列表,,执行权限表示可以把工作目录切换到此目录,删......
  • 如何在 Python 程序中读取和写入文件
     在Python编程中,文件读写是一项常见的操作。通过文件读写,我们可以从文件中读取数据,或将数据写入到文件中。本文将介绍在Python程序中进行文件读写的基本操作。 读取文件 要读取文件,我们可以使用Python内置的`open()`函数。`open()`函数接受文件路径和打开模式作为参数,并返回一......
  • Python编程该怎么实现socket文件传输
    在网络编程中,Socket是一种常用的通信协议,它可以在计算机之间进行数据传输。在Python中,我们可以使用内置的socket模块来实现Socket文件传输。本文将介绍如何使用Python编程实现Socket文件传输的步骤和示例代码。步骤一:创建服务器端首先,我们需要创建一个服务器端来接收文件。以下是创......
  • Spring Boot + MinIO 实现文件切片极速上传技术
    (文章目录)一、引言现代Web应用中,文件上传是一个常见的需求,尤其是对于大文件的上传,如视频、音频或大型文档。为了提高用户体验和系统性能,文件切片上传技术逐渐成为热门选择。本文将介绍如何使用SpringBoot和MinIO实现文件切片极速上传技术,通过将大文件分割成小片段并并行上传,显......
  • UPX 可执行文件压缩工具的介绍与使用
    UPX是什么UPX全称是"UltimatePackerforeXecutables",是一个免费、开源、编写、可扩展、高性能的可执行程序打包程序。换句话说一个可执行文件的压缩工具。主要的功能是将可执行的二进制程序、动态链接库和其他的二进制文件压缩为更小的体积,UPX通常可以将文件大小减少50%......
  • 常见的文件后缀对应的ContentType
    #LicensedtotheApacheSoftwareFoundation(ASF)underoneormore#contributorlicenseagreements.SeetheNOTICEfiledistributedwith#thisworkforadditionalinformationregardingcopyrightownership.#TheASFlicensesthisfiletoYouunderth......
  • 群晖(Synology)Plex 的服务找不到文件夹
    当Plex在搜索NAS上的文件夹的时候找不到文件夹中的内容。如下图中显示的内容。上面的Public文件夹中找不到我们的子文件夹,但是我们的子文件夹是有内容的。问题和解决出现上面的问题主要还是权限的问题。选择需要访问的文件夹,然后在文件夹上选择上面的编辑按钮。 ......
  • YOLOv5:将模型预测结果保存为Labelme格式的Json文件
    YOLOv5:将模型预测结果保存为Labelme格式的Json文件前言前提条件相关介绍实验环境YOLOv5:将模型预测结果保存为Labelme格式的Json文件代码实现进行预测输出结果前言由于本人水平有限,难免出现错漏,敬请批评改正。更多精彩内容,可点击进入Python日常小操作专栏、OpenCV-Python小应用专......