question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to crop image and disable uploading by returning "false" from "beforeUpload"

See original GitHub issue

I am using the Upload component as a local file input and am not using it to actually upload my files. I need to handle the file uploads myself. I am doing this by returning false from beforeUpload. However, if I do this while using antd-img-crop, the cropping functionality does not work.

This is my code:

...

const ImageInput: React.FC<ImageInputProps> = ({ maxCount, onChange }) => {
  const handleChange = { ... };
  const handleRemove = { ... };
  const handlePreview = { ... };
  
  const beforeUpload = () => {
    return false;
  };

  return (
    <ImgCrop grid rotate>
      <Upload
        accept='image/png, image/jpeg'
        listType='picture-card'
        onChange={handleChange}
        onRemove={handleRemove}
        onPreview={handlePreview}
        maxCount={maxCount}
        beforeUpload={beforeUpload}
      >
        <div>
          <Text type='secondary'>
            <PlusOutlined />
          </Text>
          <div style={{ marginTop: 8 }}>
            <Text type='secondary'>Upload Image</Text>
          </div>
        </div>
      </Upload>
    </ImgCrop>
  );
};

export default ImageInput;

The cropping only works if I remove beforeUpload={beforeUpload}. I’ve tried returning a lot of different things from beforeUpload, and nothing has worked.

Any ideas? Any help would be greatly appreciated. I took a look a the source code and I think the issue is that on this line it is just rejecting. The newFile at this point is just being lost. I’m not sure what to do about it, though.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
nanxiaobeicommented, Mar 17, 2021

@nanxiaobei So as an example, in beforeUpload I could set the file or fileList to the local component state and then pass that to the Upload fileList property? And then return false from beforeUpload to prevent the XMLHttpRequest?

You can write beforeUpload in this way:

const [fileList, setFileList] = useState([]);

const beforeUpload = (file) => {
  const reader = new FileReader();
  reader.readAsDataURL(file);
  reader.onload = () => {
    setFileList((prev) => [...prev, { url: reader.result }]);
  };

  // then upload `file` from the argument manually
  return false;
};

and remove onChange={onChange} to update fileList manually.

0reactions
samyarkdcommented, Nov 26, 2022

There is a problem with this solution. The property called originFileObj in added file will be undefined. How can we achieve it because the url is base64. Also, it’s not type safe and i get many type errors.

@nanxiaobei So as an example, in beforeUpload I could set the file or fileList to the local component state and then pass that to the Upload fileList property? And then return false from beforeUpload to prevent the XMLHttpRequest?

You can write beforeUpload in this way:

const [fileList, setFileList] = useState([]);

const beforeUpload = (file) => {
  const reader = new FileReader();
  reader.readAsDataURL(file);
  reader.onload = () => {
    setFileList((prev) => [...prev, { url: reader.result }]);
  };

  // then upload `file` from the argument manually
  return false;
};

and remove onChange={onChange} to update fileList manually.

Read more comments on GitHub >

github_iconTop Results From Across the Web

antd-img-crop - npm
An image cropper for Ant Design Upload. Latest version: 4.5.2, ... Start using antd-img-crop in your project by running `npm i antd-img-crop`.
Read more >
action function is required with antd upload control, but I dont ...
Upload files manually after beforeUpload returns false . beforeUpload(file) { const isJPG = file.type === 'image/jpeg' ...
Read more >
Upload - Ant Design
Click to upload user's avatar, and validate size and format of picture with beforeUpload . ... Upload files manually after beforeUpload returns false...
Read more >
Handling image uploads | Docs - TinyMCE
How to manage asynchronous image uploads. ... If the callback function provided returns false for an image, the image will not be uploaded....
Read more >
Uploading and Resizing Images with React: Part 1 - Client Side
During my development experience on a specific project, I was unable to find any good tutorials or guides on how to make a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found