繁体   English   中英

如何从使用 Ionic Framework 中的 Camera Plugin 选择的视频文件 URI 创建 Blob?

[英]How to create a Blob from video File URI selected using Camera Plugin in Ionic Framework?

在我的 Ionic 3 应用程序中,我使用相机插件从图库中选择视频文件。

它将视频作为FILE_URI ,我需要从该FILE_URI创建一个Blob

我正在使用文件插件来实现这一点。

以下是我到目前为止的代码。

const options: CameraOptions = {
    mediaType: this.camera.MediaType.VIDEO,
    sourceType: this.camera.PictureSourceType.PHOTOLIBRARY
};
this.camera.getPicture(options)
    .then(async (videoData) => {

        if (videoData) {

            const filename = videoData.substr(videoData.lastIndexOf('/') + 1);
            let dirpath = videoData.substr(0, videoData.lastIndexOf('/') + 1);
            dirpath = dirpath.includes("file://") ? dirpath : "file://" + dirpath;

            try {
                this.file.readAsArrayBuffer(dirpath, filename)
                    .then((res) => {
                        try {
                            const blob = new Blob([res], { type: 'video/mp4' });
                        } catch (error) {
                            // TODO: Handle error
                        }
                    }).catch((err) => {
                        // TODO: Handle error
                    });
            } catch (error) {
                // TODO: Handle error
            }
        }
    }, (err) => {
        // TODO: Handle error
    });

我需要知道是否可以从使用Camera Plugin选择的FILE_URI创建Blob ,如果不可能,则接受任何建议。

您可以使用File.ReadAsDataURL 尝试这样的事情:

const options: CameraOptions = {
  mediaType: this.camera.MediaType.VIDEO,
  sourceType: this.camera.PictureSourceType.PHOTOLIBRARY
};

this.camera.getPicture(options).then(async (videoData) => {
  if (videoData) {
    const filename = videoData.substr(videoData.lastIndexOf('/') + 1);
    let dirpath = videoData.substr(0, videoData.lastIndexOf('/') + 1);
    dirpath = dirpath.includes("file://") ? dirpath : "file://" + dirpath;

    try {
      this.file.readAsDataURL(dirpath, filename).then(blob => {
        uploadBlobToServer(blob);

      }).catch((err) =>              
           // TODO: Handle error
      });
     } catch (error) {
         // TODO: Handle error
     }
  }
}, (err) => {
    // TODO: Handle error
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM