【发布时间】:2019-11-19 17:14:33
【问题描述】:
我有这段代码允许用户从库中选择一个视频,并返回包含视频 uri、持续时间、类型、宽度的结果。
我想要做的是将此视频上传到Parse 服务器,我尝试了很多解决方案,但每次我都遇到同样的错误
选择视频的代码
import * as ImagePicker from "expo-image-picker";
let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.All,
allowsEditing: true,
aspect: [4, 3]
});
if (!result.cancelled) {
this.setState({
image: result.uri
});
}
选择视频后的结果:
Object {
"cancelled": false,
"duration": 4716.66650390625,
"height": 720,
"type": "video",
"uri": "file:///var/mobile/Containers/Data/Application/C925E9FC-54F4-470D-97A6-F1D72D396151/Library/Caches/ExponentExperienceData/%2540yousefalsbaihi%252FFUNTime_Beta/ImagePicker/74FF55E0-4653-4856-BF39-A67ECB3B560C.mov",
"width": 1280,
}
保存到解析中
let base64 = this.state.image
imageFile = new Parse.File("Video.mp4", base64);
imageFile.save().then(
function() {
posts.set("postFile", imageFile, "video/mp4");
posts.save().then(posts => {
Alert("DONE")
});
},
function(error) {}
);
我每次遇到的错误:
[Unhandled promise rejection: TypeError: Cannot create a Parse.File with that data.]
- node_modules/parse/lib/react-native/ParseFile.js:170:28 in ParseFile
* src/screens/AddPost.js:689:39 in <unknown>
- node_modules/promise/setimmediate/core.js:37:14 in tryCallOne
- node_modules/promise/setimmediate/core.js:123:25 in <unknown>
- ... 8 more stack frames from framework internals
我尝试过的解决方案:
- 将视频转换为Base64
我尝试在数据中添加“base64”也没有用 我还尝试添加“video/mp4;base64”,也出现了同样的错误,即使使用此代码将视频转换为 base 64。
_videoTo64 = async videoURL => {
const options = { encoding: FileSystem.EncodingType.Base64 };
const data = await FileSystem.readAsStringAsync(videoURL, options);
let vids = "base64:"+ data;
console.log(vids);
this.setState({
image: vids
});
};
- 尝试从视频 uri 中删除“file://”,这也导致了同样的错误
image.replace("file://", "")
即使我将解析保存文件更新为
imageFile = new Parse.File("Video.mp4", this.state.image);
我用的是Expo平台,图片转base64后上传正常,但是视频是问题。
【问题讨论】:
标签: react-native parse-platform expo react-native-ios