使用 putString 调用 React-native Firebase 存储上传
React-native Firebase storage upload using putString call
我设置这个线程是为了澄清,firebase 存储 putString 方法 does 还是 does not 在 React-native 中工作。
根据我的搜索,目前无法使用 put 方法将文件或 Blob 类型上传到 Firebase 存储。
React Native does not support the File and Blob types, so Firebase Storage uploads will not work in this environment. File downloads do work however.
因此这个调用
firebase.storage().ref()
.child(userID)
.put(new File(['this is a small amount of data'], 'sample-text.txt', { type: "text/plain" }), { type: "text/plain" })
.then(p => {console.log(p)})
.catch(p => {console.log(p)})
不起作用并以响应结束
code : "storage/unknown" message : "Firebase Storage: An unknown error
occurred, please check the error payload for server response." name :
"FirebaseError" serverResponse : "Multipart body does not contain 2 or
3 parts."
不过,还有另一种方法可以使用 Firebase 存储 putString 方法将数据上传到 Firebase 存储。适用于纯字符串。但是即使我用这个方法上传。我收到与以前相同的服务器响应。
firebase.storage()
.ref()
.child(userID)
.putString('string')
.then(p => {console.log(p)})
.catch(p => {console.log(p)});
Bu 从我从 中学到的东西。 putString 方式 应该 工作。
我做错了什么?该代码在 React 中对我来说工作正常。每当我粘贴到 React-native 时。它停止工作。
我刚刚在示例中尝试了 react-native-fetch-blob as Ven commented before, and I was able to make it work, try using this snippet from the index file:
1) class 声明前:
import RNFetchBlob from 'react-native-fetch-blob';
const Blob = RNFetchBlob.polyfill.Blob;
window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest;
window.Blob = Blob;
2) 存储方法内部:
let filePath = 'YOUR/FILE/PATH';
let fileName = 'file_name.jpg';
let rnfbURI = RNFetchBlob.wrap(filePath);
// create Blob from file path
Blob
.build(rnfbURI, { type : 'image/png;'})
.then((blob) => {
// upload image using Firebase SDK
firebase.storage()
.ref('images')
.child(fileName)
.put(blob, { contentType : 'image/jpg' })
.then((snapshot) => {
console.log('Uploaded', snapshot.totalBytes, 'bytes.');
console.log(snapshot.metadata);
var url = snapshot.metadata.downloadURLs[0];
console.log('File available at', url);
}).catch(function(error) {
console.error('Upload failed:', error);
});
我设置这个线程是为了澄清,firebase 存储 putString 方法 does 还是 does not 在 React-native 中工作。
根据我的搜索,目前无法使用 put 方法将文件或 Blob 类型上传到 Firebase 存储。
React Native does not support the File and Blob types, so Firebase Storage uploads will not work in this environment. File downloads do work however.
因此这个调用
firebase.storage().ref()
.child(userID)
.put(new File(['this is a small amount of data'], 'sample-text.txt', { type: "text/plain" }), { type: "text/plain" })
.then(p => {console.log(p)})
.catch(p => {console.log(p)})
不起作用并以响应结束
code : "storage/unknown" message : "Firebase Storage: An unknown error occurred, please check the error payload for server response." name : "FirebaseError" serverResponse : "Multipart body does not contain 2 or 3 parts."
不过,还有另一种方法可以使用 Firebase 存储 putString 方法将数据上传到 Firebase 存储。适用于纯字符串。但是即使我用这个方法上传。我收到与以前相同的服务器响应。
firebase.storage()
.ref()
.child(userID)
.putString('string')
.then(p => {console.log(p)})
.catch(p => {console.log(p)});
Bu 从我从
我做错了什么?该代码在 React 中对我来说工作正常。每当我粘贴到 React-native 时。它停止工作。
我刚刚在示例中尝试了 react-native-fetch-blob as Ven commented before, and I was able to make it work, try using this snippet from the index file:
1) class 声明前:
import RNFetchBlob from 'react-native-fetch-blob';
const Blob = RNFetchBlob.polyfill.Blob;
window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest;
window.Blob = Blob;
2) 存储方法内部:
let filePath = 'YOUR/FILE/PATH';
let fileName = 'file_name.jpg';
let rnfbURI = RNFetchBlob.wrap(filePath);
// create Blob from file path
Blob
.build(rnfbURI, { type : 'image/png;'})
.then((blob) => {
// upload image using Firebase SDK
firebase.storage()
.ref('images')
.child(fileName)
.put(blob, { contentType : 'image/jpg' })
.then((snapshot) => {
console.log('Uploaded', snapshot.totalBytes, 'bytes.');
console.log(snapshot.metadata);
var url = snapshot.metadata.downloadURLs[0];
console.log('File available at', url);
}).catch(function(error) {
console.error('Upload failed:', error);
});