【问题标题】:Upload video to Parse platform with React Native使用 React Native 将视频上传到 Parse 平台
【发布时间】: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

我尝试过的解决方案:

  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


    【解决方案1】:

    你没有在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 });
    

    【讨论】:

    • 请仔细阅读我的问题,我已经说过我之前尝试过这些解决方案。
    • @Balalen 您尚未指定类型。看我的回答。
    • 我在一个视频中实现,你不必指定文件类型,它会这样:` imageFile = new Parse.File("Video.mp4", { base64: theConvertedBase64 }) ; `
    • @Balalen 指定类型和编码base64是完全不同的。
    【解决方案2】:

    只要有人提出这个问题,我就这样解决了

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

    解决方案是把{ base64: theConvertedBase64 }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-17
      • 2021-09-18
      • 1970-01-01
      • 2019-04-28
      • 2020-08-16
      • 2020-03-06
      • 2021-02-27
      • 2021-09-21
      相关资源
      最近更新 更多