iT邦幫忙

2023 iThome 鐵人賽

DAY 29
0
SideProject30

Electron Angular軟體架構與簡易功能實作學習路程實記系列 第 29

Day 29 - 功能開發-14-增加圖片紀錄功能

  • 分享至 

  • xImage
  •  

圖片上傳與圖片顯示UI設計

<div class="flex justify-content-start">
        <label class="p-2">景點圖片紀錄:</label>
      </div>
      <div class="flex justify-content-start">
        <p-galleria [value]="images" [showIndicators]="true" [showIndicatorsOnItem]="true" [showThumbnails]="false" [responsiveOptions]="responsiveOptions" [containerStyle]="{ 'width': '640px' }" [numVisible]="5">
          <ng-template pTemplate="item" let-image>
            <img [src]="image.data" style="width: 100%; height: 500px" />
          </ng-template>
        </p-galleria>
      </div>
      <div class="flex justify-content-start">
        <label class="p-2">圖片紀錄:由下列選取圖片上傳.</label>
      </div>
      <div class="flex justify-content-start">
        <p-fileUpload #fileUpload [multiple]="true" [showUploadButton]="false" [fileLimit]="2" [auto]="false" accept="image/*" maxFileSize="1000000" STYLE="width: 700px">
          <ng-template pTemplate="content">
            <ul *ngIf="uploadedFiles.length">
              <li *ngFor="let file of uploadedFiles">{{ file.name }} - {{ file.size }} bytes</li>
            </ul>
          </ng-template>
        </p-fileUpload>
      </div>

前端存取圖片資料

updatePicture() {
    if (this.articleId !== undefined && this.articleId !== null) {
      this.pictureService.getPictures(this.articleId.toString()).subscribe((imageDatas: ImageData[]) => {
        this.images = imageDatas;
      });
    }
  }

  onUpload($event: any) {
    const files: File[] = $event.files;
    if (this.articleId !== undefined && this.articleId != null) {
        this.pictureService.uploadPicture(files,this.articleId.toString()).subscribe((data: any) => {

          this.messageService.add({severity: 'info', summary: 'File Uploaded', detail: ''});
          this.updatePicture();
        });
    }
  }

後端圖片處理

PictureController.get('/getPicturesByArticle/:IdArticle', (req, res) => {
  const idArticle = parseInt(req.params.IdArticle);
  if (isNaN(idArticle)) {
    return res.status(400).send('Invalid idArticle');
  }
  articleDao.findArticleFileStorage(idArticle).then((article:Article | null) => {
    let imageDatas: ImageData[] = [];
    if (article != null || article!.FileStorages != null) {
      article!.FileStorages!.forEach((fileStorage: FileStorage) => {
        if (fileStorage.storedFilename != null) {
          const filepath = path.join(Path + '/images/' + fileStorage.storedFilename);
          try {
            let value = fs.readFileSync(filepath, {encoding: 'base64'});
            let imageData: ImageData = {} as ImageData;
            imageData.id = fileStorage.id;
            imageData.originalFilename = fileStorage.originalFilename;
            imageData.data = `data:image/jpeg;base64,${value}`;
            imageDatas.push(imageData);
          }
          catch (e) {
            console.log(e);
          }
        }
      });
      return res.status(200).send(imageDatas);
    } else {
      return res.status(200).send([]);
    }
  });
});

執行結果

https://ithelp.ithome.com.tw/upload/images/20231014/20124238qX4qT0YPeU.png


上一篇
Day 28 - 功能開發-13-增加google 地圖地點標記
下一篇
Day 30 - 功能開發-15-程式打包與開發總結
系列文
Electron Angular軟體架構與簡易功能實作學習路程實記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言