简体   繁体   English

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

[英]Image created without filetype - need filetype for compression - Angular 2 / Ionic 3

I am trying to compress an image client-side (Angular 2/Ionic 3) and When I log the file that is created by the camera, it says: 我正在尝试压缩客户端的图像(Angular 2 / Ionic 3),当我记录由相机创建的文件时,它说:

"type":null when it should say "type":'image/jpeg' . "type":null当它应该说"type":'image/jpeg'

I am using the Ionic camera plugin to handle taking a picture or choosing one from the photo library, after that (I assume) the file is created without a type. 我使用Ionic相机插件来处理照片或从照片库中选择一张照片,然后(我假设)创建的文件没有类型。 Every compression tool I have tried has had this problem, and I have run out of options. 我尝试过的每个压缩工具都存在此问题,并且我的选件用完了。 Is there a way to change the type of a File object? 有没有办法更改File对象的type

I created a new Blob with this method and made sure to give it image type: 我使用此方法创建了一个新的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);  

    })
}

I used it after reading the contents of the image to base64 like this and got the resulting blob with image type: 我像这样将图像的内容读取到base64后使用了它,并得到了图像类型为blob的结果:

var readerZ = new FileReader();

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

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

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