簡體   English   中英

角度圖像上傳不適用於 Internet Explorer,但它適用於 google chrome

[英]Angular image upload does not work with Internet Explorer but it working in google chrome

我已成功實施,適用於除 Internet Explorer 11 以外的所有瀏覽器。我使用的是 Internet Explorer 11,並且已實施我已成功在 Google Chrome 中上傳.jpeg、.jpg 和 .png圖像,但無法在 IE 中上傳11
此代碼在 google chrome 開發和生產中完美運行,但在 IE11 中不起作用
我可以在 Google Chrome 中上傳 jpeg、jpg、png 各種尺寸的圖片,但無法在 IE11 中上傳任何類型的圖片。

<div class="form-group">
  <label class="form-control-label" jhiTranslate="companyManagement.new-logo">Upload new Logo</label>
  <input class="tdw-display-none" (change)="onFileChange($event, 'logo')" type="file"
           formControlName="logo" placeholder="Company logo">

onFileChange($event, doc: string) {
const files = $event.target.files;
if (files && files.length > 0) {
  const fileType: string = files[0].type;
  const fileSize: number = files[0].size;
  if (fileType) {
    if (fileType.includes('png') || fileType.includes('jpeg') || fileType.includes('jpg')) {
      this.image.name = files[0].name;
      this._compressImage(files[0], fileData => {
        this.image.data = fileData;
        this._mLogo = fileData;
        this.company.logo = fileData.split(',')[1];
      });
    } else {
      console.log('--------------Invalid File Type----------');
    }
  }
}

}

private bytesToSize(bytes) {
    const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
    if (bytes === 0) {
      return 'n/a';
    }
    const i = Math.floor(Math.log(bytes) / Math.log(1024));
    if (i === 0) {
      return bytes + ' ' + sizes[i];
    }
    return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
  } 

    private _compressImage(file, callback) {
    this.ng2ImgMaxService.resize([file], 600, 600).subscribe(
      result => {
        this._readImageAndLoad(result, callback);
        console.log("after _readImageAndLoad");
      },
      error => {
        console.error('---------------Resize error-------------', error);
      }
    );
  }  

    private _readImageAndLoad(file, callback) {
    const reader = new FileReader();
    reader.onload = (e: any) => {
      const dataUrl = e.target.result;
      const base64 = dataUrl.split(',')[1];
      console.log(" _readImageAndLoad");
      callback(dataUrl);
    };
    reader.readAsDataURL(file);
  }

您正在使用箭頭函數( () => {} )。

IE11 不支持它們,因此您必須將它們更改為舊的實現方式。 例子:

  error => {
    console.error('---------------Resize error-------------', error);
  }

  //Should be

 function(error) {
    console.error('---------------Resize error-------------', error);
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM