npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ueno-img_upload

v1.0.1

Published

图片上传组件

Downloads

3

Readme

图片上传组件(基于 react、ts、ant design)

下载依赖

npm i antd

使用方法

  1. 传入 props
  2. 组件功能
    • 限制上传图片数量(目前支持一张)可扩展限制数量
    • 当前是通过获取上传路径,将文件手动上传至指定路径
    • 可通过直接指定上传地址上传图片,不必通过接口请求要上传的地址
    • props 传入 disabled 可以禁止编辑图片
  3. 修改组件配置内容
    • AdminController.getUrl 应为 自定义的请求, 通过文件名得到将要上传文件的地址
  4. 组件重要代码
    /*
     * @Description: 
     * @Author: PhilRandWu
     * @Github: https://github/PhilRandWu
     * @Date: 2022-11-19 08:31:13
     * @LastEditTime: 2023-01-05 23:23:32
     * @LastEditors: PhilRandWu
     */
    import { PlusOutlined } from "@ant-design/icons";
    import AdminController from "@services/admin";
    import { IDataResponse, IErrorDataResponse } from "@services/types/response-type";
    import { message, Modal, Upload, UploadFile } from "antd";
    import axios from "axios";
    import React, { useState } from "react";
       
       
    export interface ImgUploadInterface {
      culImgUrl?: string;
      uploadText?: string;
      onChange: (newVal: string) => void;
      disabled?: boolean;
    }
       
       
    const ImgUpload = (props: ImgUploadInterface) => {
      const [previewOpen, setPreviewOpen] = useState(false);
       
       
      const getUploadContent = () => {
        if (props.culImgUrl) {
          return null;
        } else {
          return (
            <div>
              <PlusOutlined />
              <div style={{ marginTop: 8 }}>{props?.uploadText ? props?.uploadText : '选择图片'}</div>
            </div>
          )
        }
      }
       
       
      const getFileList: () => UploadFile[] = () => {
        if (props.culImgUrl) {
          return [
            {
              uid: props.culImgUrl,
              name: props.culImgUrl,
              url: props.culImgUrl,
            }
          ]
        }
        return [];
      }
       
      const handleRequest = async p => {
       
       
        const formData = new FormData();
        formData.append(p.file?.name, p.file);
        const { faceUrl } = await AdminController.getUrl(p?.file?.name);
        const requestUrl = faceUrl?.split('?')[0];
        const result: any = { method: 'put', url: requestUrl, data: p?.file }
        axios(result).then(res => {
          if(res?.status === 200) {
            props.onChange(requestUrl)
          }
        })
      }
       
       
      return <>
        <Upload
          action="/api/upload/movie"
          // name="imageFile"
          accept=".jpg,.png,.gif,.webp,.jpeg"
          listType="picture-card"
          fileList={getFileList()}
          disabled={props?.disabled}
          customRequest={handleRequest}
          onRemove={() => {
            if(props?.disabled) {
              return false;
            } else {
              props.onChange('');
            }
          }}
          onPreview={() => {
            setPreviewOpen(true);
          }}
          >
          {getUploadContent()}
        </Upload>
        <Modal open={previewOpen} title="预览" footer={null} onCancel={() => {
        setPreviewOpen(false)
      }}>
          <img alt="example" style={{ width: '100%' }} src={props.culImgUrl} />
        </Modal>
      </>
    }
       
       
    export default ImgUpload;
    1. 父组件基础使用
    const Demo = () => {
        return (
            <ImgUpload />
        )
    }
       
    export default Demo;