使用Antd上传操作将图像上传到Firebase存储时出现问题 [英] Issues while uploading an image to firebase storage with Antd upload action
问题描述
我正在使用antd 图片墙/卡使用此参考代码以及我要更改的唯一位置将图像上传到Firebase存储的示例是<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.
在action
属性上,我正在使用一个函数,将图像上载到Firebase存储中,而不是链接两者都可以接受,如在文档中看到的.
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.
我的动作函数如下:
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)
}
问题来了,图像已成功上传到Firebase,但是我不断收到蚂蚁般的响应处理错误,并且可能无法确定action
函数应返回的内容,即使在文档中也写到了它应该返回一个诺言.
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.
错误消息:
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.
请求的帮助,我的操作功能应该返回什么以消除错误.我可以解析我的Firebase响应,并将必要的详细信息返回给antd上传操作.
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.
使用
"antd": "^3.9.2",
"firebase": "^5.8.5",
"react": "^16.7.0",
推荐答案
您可以使用customRequest
属性来解决此问题.看看
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>
);
}
}
这篇关于使用Antd上传操作将图像上传到Firebase存储时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!