使用React Native将视频上传到Parse平台

问题描述 投票:0回答:2

我有此代码,允许用户从库中选择视频,并将返回包含视频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

我尝试过的解决方案:

  1. 将视频转换为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
        });
      };

  1. [试图从视频uri中修剪'file://',这也导致了相同的错误

image.replace("file://", "")

即使我将解析保存文件更新为

imageFile = new Parse.File("Video.mp4", this.state.image);

我正在使用Expo平台,将图像转换为base64后,图像上传工作正常,但是视频是问题所在。

react-native parse-platform expo react-native-ios
2个回答
0
投票

您未在base64中进行编码,因此当前用法不正确。

[Parse将根据文件扩展名自动检测要上传的文件类型,但是您可以使用第三个参数指定Content-Type

用法

imageFile = new Parse.File("Video.mp4", this.state.image, "video/quicktime");

base64示例

var base64 = "V29ya2luZyBhdCBQYXJzZSBpcyBncmVhdCE=";
var file = new Parse.File("myfile.txt", { base64: base64 });

0
投票

对于任何人,这个问题我都这样解决了

  1. 将视频转换为base64
  2. 将视频保存在base64中,然后像这样传递以进行解析
    imageFile = new Parse.File("Video.mp4", { base64: theConvertedBase64 });

解决方案是放入{ base64: theConvertedBase64 }

© www.soinside.com 2019 - 2024. All rights reserved.