[英]Uploading an mp3 to Firebase Storage with React Native Expo
I am attempting to upload an mp3 to firebase storage using expo and react native.我正在尝试使用 expo 将 mp3 上传到 firebase 存储并做出原生反应。 So far I've got the file into firebase storage, but it's only 9bytes large, so I'm doing something wrong.
到目前为止,我已经将文件放入 firebase 存储空间,但它只有 9 个字节大,所以我做错了什么。 I've attempted this with blob as shown below with no success.
我尝试使用 blob 进行此操作,如下所示,但没有成功。
Here is a screenshot of the firebase storage folder showing the file uploaded but not the data of said file:这是 firebase 存储文件夹的屏幕截图,显示了上传的文件,但没有显示该文件的数据:
Any help is greatly appreciated, I feel like I'm missing a step to actually upload the data along with the file.非常感谢任何帮助,我觉得我错过了实际上传数据和文件的步骤。
export default function SongPicker() {
const [song, setSong] = useState(null);
//Get current user through authentication
const user = auth.currentUser;
const pickDocument = async () => {
let result = await DocumentPicker.getDocumentAsync({});
// Fetch the photo with it's local URI
const response = fetch(result.uri);
alert(result.uri);
console.log(result);
const file = new Blob(
[response.value], {
type: 'audio/mpeg'
});
console.log('do we see this?');
try {
//Create the file reference
const storage = getStorage();
const storageRef = ref(storage, `songs/${user.uid}/${result.name}`);
// Upload Blob file to Firebase
const snapshot = uploadBytes(storageRef, file, 'blob').then((snapshot) => {
console.log('Uploaded a song to firebase storage!');
});
setSong(result.uri);
} catch (error) {
console.log(error);
}
}
The fetch()
returns a Promise so you should add an await for that as well. fetch()
返回一个 Promise 所以你也应该为它添加一个 await 。
const response = await fetch(result.uri);
Then try using blob()
method on the Response :然后尝试在Response上使用
blob()
方法:
const file = await response.blob()
The third param in uploadBytes
should be upload metadata object but you can skip that here: uploadBytes
中的第三个参数应该是上传元数据 object 但你可以在这里跳过它:
const snapshot = await uploadBytes(storageRef, file).
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.