Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues while uploading an image to firebase storage with Antd upload action

I'm using antd picture-wall/card example to upload images to my firebase storage with this reference code and the only place I'm changing is action property on <Upload> component.

On the action property, I'm using a function that uploads the images to firebase storage instead of a link both are accepted as seen in docs.

My action function looks like this;

export async function uploadImage(file) {
    const storage = firebase.storage()
    const metadata = {
        contentType: 'image/jpeg'
    }
    const storageRef = await storage.ref()
    const imageName = generateHashName() //a unique name for the image
    const imgFile = storageRef.child(`Vince Wear/${imageName}.png`)
    return imgFile.put(file, metadata)
}

Issue comes, The image uploads to firebase successfully, but I keep getting antd response handling errors and possibly not sure what action function should return, even though, is written in the docs that it should return a promise.

Error message:

XML Parsing Error: syntax error
Location: http://localhost:3000/[object%20Object]
Line Number 1, Column 1:

Errors also appear as a red border on the uploaded image thumbnail.

Requested help, What should my action function return to get rid of errors. I can parse my firebase response and return the necessary details to antd upload action.

Using

    "antd": "^3.9.2",
    "firebase": "^5.8.5",
    "react": "^16.7.0",
like image 995
ArchNoob Avatar asked Apr 02 '19 10:04

ArchNoob


People also ask

Which method can we use to upload an image to Firebase Cloud Storage?

To upload a file to Cloud Storage, you first create a reference to the full path of the file, including the file name. Once you've created an appropriate reference, you then call the putBytes() , putFile() , or putStream() method to upload the file to Cloud Storage.

How do I upload images to Firebase storage flutter?

Once you've created an appropriate reference, you then call the putFile() , putString() , or putData() method to upload the file to Cloud Storage. You cannot upload data with a reference to the root of your Cloud Storage bucket. Your reference must point to a child URL.


2 Answers

You can use customRequest prop to fix this issue. Have a look

class CustomUpload extends Component {
  state = { loading: false, imageUrl: '' };
  
  handleChange = (info) => {
    if (info.file.status === 'uploading') {
      this.setState({ loading: true });
      return;
    }
    if (info.file.status === 'done') {
      getBase64(info.file.originFileObj, imageUrl => this.setState({
        imageUrl,
        loading: false
      }));
    }
  };

  beforeUpload = (file) => {
    const isImage = file.type.indexOf('image/') === 0;
    if (!isImage) {
      AntMessage.error('You can only upload image file!');
    }
    
    // You can remove this validation if you want
    const isLt5M = file.size / 1024 / 1024 < 5;
    if (!isLt5M) {
      AntMessage.error('Image must smaller than 5MB!');
    }
    return isImage && isLt5M;
  };

  customUpload = ({ onError, onSuccess, file }) => {
    const storage = firebase.storage();
    const metadata = {
        contentType: 'image/jpeg'
    }
    const storageRef = await storage.ref();
    const imageName = generateHashName(); //a unique name for the image
    const imgFile = storageRef.child(`Vince Wear/${imageName}.png`);
    try {
      const image = await imgFile.put(file, metadata);
      onSuccess(null, image);
    } catch(e) {
      onError(e);
    }
  };
  
  render () {
    const { loading, imageUrl } = this.state;
    const uploadButton = (
    <div>
      <Icon type={loading ? 'loading' : 'plus'} />
      <div className="ant-upload-text">Upload</div>
    </div>
    );
    return (
      <div>
        <Upload
          name="avatar"
          listType="picture-card"
          className="avatar-uploader"
          beforeUpload={this.beforeUpload}
          onChange={this.handleChange}
          customRequest={this.customUpload}
        >
          {imageUrl ? <img src={imageUrl} alt="avatar" /> : uploadButton}
        </Upload>
      </div>
    );
  }
}
like image 77
Zohaib Ijaz Avatar answered Sep 22 '22 14:09

Zohaib Ijaz


Just leaving this here incase anyone wanted to track the progress of the file aswell

 const customUpload = async ({ onError, onSuccess, file, onProgress }) => {
    let fileId = uuidv4()
    const fileRef = stg.ref('demo').child(fileId)
    try {
      const image = fileRef.put(file, { customMetadata: { uploadedBy: myName, fileName: file.name } })

      image.on(
        'state_changed',
        (snap) => onProgress({ percent: (snap.bytesTransferred / snap.totalBytes) * 100 }),
        (err) => onError(err),
        () => onSuccess(null, image.metadata_)
      )
    } catch (e) {
      onError(e)
    }
  }
like image 30
Jialx Avatar answered Sep 19 '22 14:09

Jialx