Firebase 存储:`put` 预期 Blob 或文件中的参数无效
Firebase Storage: Invalid argument in `put` Expected Blob or File
我正在学习教程并尝试使用网络摄像头捕获图像作为 jpeg,我想将其上传到 firebase 存储,它要求我将其作为文件或 blob 上传。我可以在使用 <input type = 'file'>
格式时上传图片,但我想直接从网络摄像头捕获的图像上传图片。有什么方法可以将 jpeg 转换为文件或 blob 吗?
这是代码
import React, { useState } from 'react'
import Webcam from 'react-webcam'
import firebase from 'firebase'
const WebcamComponent = () => <Webcam/>
const videoConstraints = {
width: 220,
height: 200,
facingMode: 'user'
}
const WebcamCapture = () => {
const webcamRef = React.useRef(null)
const [image, setImage] = useState('')
const capture = React.useCallback(
() => {
const imageSrc = webcamRef.current.getScreenshot()
setImage(imageSrc)
const file = image
var storage = firebase.storage()
storage.ref('FormPhotos/' + file.name).put(file).on('state_changed', alert('success'), alert)
console.log(image)
},
[webcamRef]
)
return (
<div className = 'webcam-container'>
<div className = 'webcam-img'>
{
image == '' ? <Webcam
audio = {false}
height = {200}
ref = {webcamRef}
screenshotFormat = 'image/jpeg'
width = {220}
videoConstraints = {videoConstraints}
/> : <img src = {image}/>}
</div>
<div>
{
image != ''
? <button onClick = {(e) => {
e.preventDefault()
setImage('')
}} className = 'webcam-btn'>
Retake Image
</button>
: <button onClick = {(e) => {
e.preventDefault()
capture()
}}>Capture</button>
}
</div>
</div>
)
}
export default WebcamCapture
getScreenshot()
方法 returns 当前网络摄像头图像的 base64 编码字符串。使用 putString
方法而不是 put
并在其中传递 imageSrc
:
const imageSrc = webcamRef.current.getScreenshot()
const base64String = imageSrc.split(',')[1]
var storage = firebase.storage()
storage
.ref('FormPhotos/' + file.name)
.putString(base64string, "base64", {contentType: 'image/jpeg'})
.then(() => {
console.log("Image uploaded")
}).catch((e) => console.log(e))
If no contentType metadata is specified and the file doesn't have a file extension, Cloud Storage defaults to the type application/octet-stream.
我正在学习教程并尝试使用网络摄像头捕获图像作为 jpeg,我想将其上传到 firebase 存储,它要求我将其作为文件或 blob 上传。我可以在使用 <input type = 'file'>
格式时上传图片,但我想直接从网络摄像头捕获的图像上传图片。有什么方法可以将 jpeg 转换为文件或 blob 吗?
这是代码
import React, { useState } from 'react'
import Webcam from 'react-webcam'
import firebase from 'firebase'
const WebcamComponent = () => <Webcam/>
const videoConstraints = {
width: 220,
height: 200,
facingMode: 'user'
}
const WebcamCapture = () => {
const webcamRef = React.useRef(null)
const [image, setImage] = useState('')
const capture = React.useCallback(
() => {
const imageSrc = webcamRef.current.getScreenshot()
setImage(imageSrc)
const file = image
var storage = firebase.storage()
storage.ref('FormPhotos/' + file.name).put(file).on('state_changed', alert('success'), alert)
console.log(image)
},
[webcamRef]
)
return (
<div className = 'webcam-container'>
<div className = 'webcam-img'>
{
image == '' ? <Webcam
audio = {false}
height = {200}
ref = {webcamRef}
screenshotFormat = 'image/jpeg'
width = {220}
videoConstraints = {videoConstraints}
/> : <img src = {image}/>}
</div>
<div>
{
image != ''
? <button onClick = {(e) => {
e.preventDefault()
setImage('')
}} className = 'webcam-btn'>
Retake Image
</button>
: <button onClick = {(e) => {
e.preventDefault()
capture()
}}>Capture</button>
}
</div>
</div>
)
}
export default WebcamCapture
getScreenshot()
方法 returns 当前网络摄像头图像的 base64 编码字符串。使用 putString
方法而不是 put
并在其中传递 imageSrc
:
const imageSrc = webcamRef.current.getScreenshot()
const base64String = imageSrc.split(',')[1]
var storage = firebase.storage()
storage
.ref('FormPhotos/' + file.name)
.putString(base64string, "base64", {contentType: 'image/jpeg'})
.then(() => {
console.log("Image uploaded")
}).catch((e) => console.log(e))
If no contentType metadata is specified and the file doesn't have a file extension, Cloud Storage defaults to the type application/octet-stream.