我正在开发一个实时离子应用程序。为了捕获音频/图像,我正在使用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];


  }

}

最佳答案

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

 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);
    })
})


}

关于javascript - 如何将MediaFile对象转换为File对象?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52373258/

10-09 17:37