Angular + ABP 上传图片

angular

1,html页面通过input上传图片

html:

<img *ngFor="let imageItem of imageUrlList" [src]="imageItem" alt="Image"/>

<input nz-input type="file" name="image" accept=".jpg,.png,jpeg" (change)="handleChange($event)"/>
                                                  

2,ts 将上传的文件捕获。其中getBase64方法读取文件数据。sanitizer.bypassSecurityTrustUrl方法返回安全URL,预防反编译。btoa方法将文件对象编码为string类型保存到后端数据库。

import { DomSanitizer, SafeUrl} from '@angular/platform-browser';

constructor( private sanitizer: DomSanitizer ) {}


imageUrlList: SafeUrl[] = [];
objectDto: ObjectDto;

handleChange(event: any ): void {
    if (event && event.target && event.target.files) {
      if (event.target.files.length === 0) {
          console.log('No file selected!');
          return;
      }
      const file = event.target.files[0];
      this.getBase64(file, (img: string) => {
        if (this.imageUrlList === undefined) {
          this.imageUrlList = [];
        }
        this.imageUrlList.push(this.sanitizer.bypassSecurityTrustUrl(img));
        if (this.objectDto.setUpPhotoList === undefined) {
          this.objectDto.setUpPhotoList = [];
        }
        this.objectDto.setUpPhotoList.push(btoa(img));
      });
    }
  }

  getBase64(img: File, callback: (img: string) => void): void {
    const reader = new FileReader();
    reader.addEventListener('load', () => callback(reader.result!.toString()));
    reader.readAsDataURL(img);
  }

3,ts 从数据库获取的图片并展示。atob方法解码,然后输出URL供前端img调用显示。

if (this.objectDto.setUpPhotoList !== undefined && this.objectDto.setUpPhotoList !== null && this.objectDto.setUpPhotoList.length > 0) {
      this.objectDto.setUpPhotoList.forEach(item => {
      // 解码
      const Url = atob(item);
      this.imageUrlList.push(this.sanitizer.bypassSecurityTrustUrl(Url));
    }) 
}

ABP

1,实例中Dto和Entity的属性值相同,可以使用autoMap标签自动映射。SetUpPhotoList 的类型是List<string>,图片在数据库中的保存类型为string类型。

 public class objectDto
 {
        public List<string> SetUpPhotoList { get; set; }
 }

 public class objectEntity
 {
        public List<string> SetUpPhotoList { get; set; }
 }

2,配置文件如下,在保存到数据库时将List<string>序列化,读取数据库时再反序列化。

public class objectEntityConfiguration : IEntityTypeConfiguration<objectEntity>
    {
        public void Configure(EntityTypeBuilder<objectEntity> builder)
        {
            builder.ToTable("objectEntity");

            builder.Property(p => p.SetUpPhotoList)
                .HasConversion(
                    v => JsonConvert.SerializeObject(v),
                    v => JsonConvert.DeserializeObject<List<string>>(v));
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Angular是一个流行的JavaScript框架,用于构建Web应用程序。Ionic是基于Angular的开源移动应用开发框架,它提供了一套UI组件和具,帮助开发者构建跨平台的移动应用程序。 Swiper是一个流行的移动端滑动组件库,它提供了丰富的滑动效果和交互功能,可以用于创建漂亮的轮播图、图片浏览器等。 结合Angular和Ionic,你可以轻松地集成Swiper组件到你的移动应用中。首先,你需要在你的Angular项目中安装Swiper组件库。可以使用npm命令来安装: ``` npm install swiper --save``` 安装完成后,你可以在你的Ionic组件中引入Swiper组件,并在模板中使用它。以下是一个简单的示例: ```typescriptimport { Component } from '@angular/core'; import SwiperCore, { Navigation, Pagination } from 'swiper/core'; SwiperCore.use([Navigation, Pagination]); @Component({ selector: 'app-swiper', template: ` <swiper [navigation]="true" [pagination]="true"> <ng-template swiperSlide>Slide1</ng-template> <ng-template swiperSlide>Slide2</ng-template> <ng-template swiperSlide>Slide3</ng-template> </swiper> `, }) export class SwiperComponent {} ``` 在上面的示例中,我们首先引入了Swiper组件库,并注册了所需的Swiper模块(例如Navigation和Pagination)。然后,在组件的模板中,我们使用`<swiper>`标签创建了一个Swiper实例,并在内部添加了三个滑动的内容块。 你可以根据你的需求自定义Swiper的配置和样式。更多关于Swiper的用法和配置,你可以参考Swiper官方文档。 希望这可以帮助到你!如果你还有其他问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值