[英]How to Upload Images to Firebase Storage and Get Image's Download URL's When Using Dropzone in React.js
I have a <Dropzone/>
component in my react website that allows my users to upload multiple images just by either clicking or dragging and dropping.我的反应网站中有一个
<Dropzone/>
组件,它允许我的用户通过单击或拖放来上传多个图像。
When I upload the images they are being saved in my firebase storage but the URL's for each indiviual image are not being set and i get a firebase error that reads:当我上传图像时,它们被保存在我的 firebase 存储中,但没有设置每个单独图像的 URL,我收到 firebase 错误,内容如下:
"Uncaught (in promise) FirebaseError: Firebase Storage: Object 'propertyImages/Picture1.png+4b80d-6dd0-4b40-cadc-8e7845677bbd' does not exist. (storage/object-not-found)
{
"error": {
"code": 404,
"message": "Not Found."
}
}"
I don't know why it isn't setting the url for each image, i believe it is because it is overwritting the previous download urls.我不知道为什么它没有为每个图像设置 url,我相信这是因为它覆盖了以前的下载 url。 How can i make Dropzone work properly?
如何使 Dropzone 正常工作?
What am I doing wrong?我究竟做错了什么?
Here is my code:这是我的代码:
import Dropzone, {useDropzone} from "react-dropzone"
const [urls,setUrls] = useState([]);
const { getRootProps, getInputProps, isDragActive } = useDropzone({ onDropAccepted: onFilesDrop, maxFiles: 5 });
function onFilesDrop(files) {
files.map((item, index) => {
let uploadTask = storage.ref(`propertyImages/${item.name}+${uuid()}`).put(item);
uploadTask.snapshot.ref.getDownloadURL().then(function (downloadURL) {
setUrls(prevState => {
return [...prevState, downloadURL]
})
})
})
}
return (
<CardTitle className="mb-3 h4">Property Images (MAX IMAGES 5)</CardTitle>
<div className="dropzone" >
<Dropzone
onDrop={acceptedFiles =>
onFilesDrop(acceptedFiles)
}
>
{({ getRootProps, getInputProps }) => (
<div>
<div
className="dz-message needsclick"
{...getRootProps()}
>
<input {...getInputProps()} />
<div className="dz-message needsclick">
<div className="mb-3">
<i className="display-4 text-muted bx bxs-cloud-upload" />
</div>
<h4>Drop files here or click to upload.</h4>
</div>
</div>
</div>
)}
</Dropzone>
)
update onFilesDrop function更新文件删除 function
function onFilesDrop(files) {
files.map((item, index) => {
let uploadTask = storage.ref(`propertyImages/${item.name}+${uuid()}`).put(item);
console.log(uploadTask);
uploadTask.on(
"state_changed",
snapshot => {
},
error => {
console.log(error);
},
async () => {
await storage
.ref("images")
.child(item.name)
.getDownloadURL()
.then(urls => {
console.log(urls) //log url if you want
setUrls((P) => [...P, urls]);
})
}
)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.