【问题标题】:How Do I Replace UploadCare To Display Image in React如何替换 UploadCare 以在 React 中显示图像
【发布时间】:2021-09-03 03:52:00
【问题描述】:

现在,上传图片后,它只显示文件名、大小和删除按钮。我想在圆圈上显示图像本身。

请在此处检查代码和框 CLICK HERE

<UploadWrapper>
    <Widget
      localeTranslations={translation}
      publicKey="demopublickey"
      imagesOnly
      previewStep
      clearable
      onfi
      onFileSelect={(file) => {
        if (!file) {
          console.log("File removed from widget");
          return;
        }
        file.done((fileInfo) => {
          console.log("File uploaded: ", fileInfo.cdnUrl);
        });
      }}
    />
  </UploadWrapper>

【问题讨论】:

    标签: reactjs react-hooks uploadcare


    【解决方案1】:

    据我所知,Widget 组件似乎仅用于上传文件而不用于渲染文件。

    这是我想出的一个解决方案:使用一些“状态”来存储上传的文件 URL,并有条件地隐藏 Widget 以上传文件或渲染的 div/图像。在图像上的单击处理程序中使用附加到 Widget 的引用来触发重新打开上传对话框。

    风格:

    import styled, { css } from "styled-components";
    
    ...
    
    const UploadWrapper = styled.div`
      position: relative;
      display: flex;
      justify-content: center;
      align-items: center;
    
      ${({ image }) => image ? css`
        & .image {
          position: absolute;
          height: 300px;                      // make a circle
          width: 300px;                       // make a circle
          border-radius: 50%;                 // make a circle
          top: 50%;                           // position div
          overflow: hidden;                   // hide the circle 
    
          img {
            height: 100%;                     // fill parent div
            width: auto;                      // fill parent div
            transform: translate(-50%, -50%); // center image
            top: 50%;                         // center image
            left: 50%;                        // center image
            position: absolute;
          }
        }
    
        & .uploadcare--widget {
          display: none;
        }
      ` : css`
        & .image {
          display: none;
        }
      `}
    
      & .uploadcare--widget__button_type_open {
        height: 300px;
        width: 300px;
        border-radius: 100%;
      }
    `;
    

    组件:

    const Upload = () => {
      const [image, setImage] = React.useState("");
    
      const translation = {
        buttons: {
          choose: {
            images: {
              one:
                '<div className="image"><img src="/assets/images/camera.svg" alt="camera" /><br/>Upload photo</div>'
            }
          }
        }
      };
    
      return (
        <FormWrapper>
          <h1>Hello</h1>
          <UploadWrapper image={image}>
            <div
              className="image"
              onClick={() => widgetApi.current.openDialog()}
            >
              <img src={image} alt="uploaded" />
            </div>
            <Widget
              localeTranslations={translation}
              publicKey="demopublickey"
              imagesOnly
              previewStep
              onFileSelect={(file) => {
                if (!file) {
                  console.log("File removed from widget");
                  return;
                }
                file.done((fileInfo) => {
                  setImage(fileInfo.cdnUrl);
                });
              }}
            />
          </UploadWrapper>
        </FormWrapper>
      );
    };
    

    【讨论】:

    • 谢谢德鲁。但是上传后我不能再选择图像了?它应该仍然是可点击的,它将打开上传器
    • @Joseph 嗯,似乎你可以在.image div 中添加一个 onClick 来切换图像状态并将其设置回来并渲染Widget。这将需要删除 .image 元素上的 pointer-events: none; 规则。如果您想看看,我在沙盒中应用了此更改。
    • 谢谢。点击后可以打开上传器吗?
    • @Joseph 是的。花了一些时间研究文档,但我想我有一些对你有用的东西。
    • 好的,我认为现在就足够了。谢谢德鲁
    猜你喜欢
    • 2020-09-28
    • 2021-07-31
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 2018-11-07
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多