创建的图像没有文件类型 - 需要压缩文件类型 - Angular 2 / Ionic 3

时间:2017-07-22 23:14:46

标签: image angular file ionic-framework types

我正在尝试压缩图像客户端(Angular 2 / Ionic 3),当我记录相机创建的文件时,它说:

"type":null应该说"type":'image/jpeg'

我正在使用Ionic相机插件处理拍照或从照片库中选择一个,之后(我假设)文件是在没有类型的情况下创建的。我尝试过的每个压缩工具都有这个问题,而且我的选项已经用完了。有没有办法更改type对象的File

1 个答案:

答案 0 :(得分:1)

我用这种方法创建了一个新的Blob,并确保给它图像类型:

dataURItoBlob(dataURI, callback): Promise<any> {
    return new Promise((resolve, reject) => {
      let byteString = atob(dataURI);
      //console.log(byteString);

      // separate out the mime component
      //let mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]

      // write the bytes of the string to an ArrayBuffer
      let ab = new ArrayBuffer(byteString.length);
      let ia = new Uint8Array(ab);
      for (let i = 0; i < byteString.length; i++) {
          ia[i] = byteString.charCodeAt(i);
      }

      // write the ArrayBuffer to a blob, and you're done
      let bb = new Blob([ab], {type: 'image/jpeg'});
      resolve(bb);  

    })
}

我在将图像的内容读取到这样的base64之后使用它,并得到了带有图像类型的结果blob:

var readerZ = new FileReader();

  readerZ.onload = (e) => {
    let data = readerZ.result.split(',')[1];
    //console.log(data);
    self.dataURItoBlob(data, null).then(blob => {