如何将MediaFile对象转换为File对象?

时间:2018-09-17 17:48:40

标签: javascript cordova ionic-framework media record

我正在开发实时离子应用程序。为了捕获音频/图像,我正在使用cordova插件媒体捕获。我将能够通过api post multipart-form-data发送文件对象MediaFile(mediaFile)。我需要将MediaFile Object JS转换为File Object JS(Api multipart post仅接受File,而不接受MediaFile)

$scope.captureImage = function() {
      navigator.device.capture.captureImage(captureSuccessImage,captureErrorImage);
}

function captureSuccessImage(mediaFiles) {

  var i, mediaFile, len;

  for (i = 0, len = mediaFiles.length; i < len; i += 1) {

      mediaFile = mediaFiles[i];


  }

}

2 个答案:

答案 0 :(得分:1)

它还将支持媒体路径。我正在使用此文件传输代码。请看看。

 postFile(imageData, id) {
let base64Image = imageData;
this.filePath.resolveNativePath(imageData)
  .then(filePath => base64Image)
  .catch(err => console.log(err));
console.log(base64Image);
const fileTransfer = this.transfer.create();
let imageName = base64Image;
var options: FileUploadOptions = {
  fileKey: "file",
  fileName: imageName.substr(imageName.lastIndexOf('/') + 1),
  mimeType: "multipart/form-data",
  chunkedMode: false,
  params: { 'id': id },
  headers: { 'Authorization': 'Bearer ' + localStorage.getItem("token"),  'Connection': "close" }
}
return new Promise((resolve, reject) => {
  fileTransfer.upload(imageName, encodeURI(this.MainURL + "/UploadMedia"), options)
    .then((data) => {
      console.log(data);
      resolve(200);
    }, (err) => {
      console.log(err);

      reject(500);
    })
})


}

答案 1 :(得分:0)

就我而言,我确实需要将MediaFile转换为File才能使vimeo API正常工作。 由于MediaFile是文件:

set