PhotoViewModel (photo); return Ok(result);}}} 这里要简单讲一下. asp

时间:2022-06-12 04:16:31

第三部分: https://www.cnblogs.com/cgzl/p/8525541.html

第四部分: https://www.cnblogs.com/cgzl/p/8536350.html

这部分就讲从angular5的客户端上传图片到asp.net core 2.0的 web api.

这是需要的源码: https://pan.baidu.com/s/1Eqc4MRiQDwOHmu0OHyttqA

当前的效果如下:

PhotoViewModel (photo); return Ok(result);}}} 这里要简单讲一下. asp

点击这个超链接后:

PhotoViewModel (photo); return Ok(result);}}} 这里要简单讲一下. asp

好的, 下面开始编写上传相关的代码.

Asp.net core 2.0 文件上传

凭据挨次, 先成立Photo的domain model:

成立Models/Photo.cs:

using System.ComponentModel.DataAnnotations; namespace Tv.Models { public class Photo { public int Id { get; set; } [Required] [StringLength(255)] public string FileName { get; set; } } }

然后编纂TvShow.cs:

using System.Collections.Generic; using System.ComponentModel.DataAnnotations; namespace Tv.Models { public class TvShow { public TvShow() { Photoes = new List<Photo>(); } public int Id { get; set; } [Required] [StringLength(50)] public string Name { get; set; } public int TvNetworkId { get; set; } public TvNetwork TvNetwork { get; set; } public ICollection<Photo> Photoes { get; set; } } }

TvContext.cs:

using Microsoft.EntityFrameworkCore; using Tv.Models; namespace Tv.Database { public class TvContext : DbContext { public TvContext(DbContextOptions<TvContext> options) : base(options) { } public DbSet<TvNetwork> TvNetworks { get; set; } public DbSet<TvShow> TvShows { get; set; } public DbSet<Photo> Photoes { get; set; } } }

然后添加迁移和更新数据库, 您应该知道怎么做了, 这部分就略了.

添加PhotoViewModel.cs:

namespace Tv.ViewModels { public class PhotoViewModel { public int Id { get; set; } public string FileName { get; set; } } }

不要忘了做一下Mapping映射, 这里我就不写了.

然后成立PhotoesController.cs:

using System; using System.IO; using System.Threading.Tasks; using AutoMapper; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Tv.Database; using Tv.Models; using Tv.ViewModels; namespace Tv.Controllers { [Route("api/tvshows/{tvShowId}/photoes")] public class PhotoesController : Controller { private readonly IHostingEnvironment host; private readonly ITvRepository tvRepository; private readonly IUnitOfWork unitOfWork; private readonly IMapper mapper; public PhotoesController(IHostingEnvironment host, ITvRepository tvRepository, IUnitOfWork unitOfWork, IMapper mapper) { this.host = host; this.tvRepository = tvRepository; this.unitOfWork = unitOfWork; this.mapper = mapper; } [HttpPost] public async Task<IActionResult> Upload(int tvShowId, IFormFile file) { var tvShow = await tvRepository.GetTvShowByIdAsync(tvShowId, includeRelated: false); if (tvShow == null) { return NotFound(); } var uploadsFolderPath = Path.Combine(host.WebRootPath, "Uploads"); if (!Directory.Exists(uploadsFolderPath)) { Directory.CreateDirectory(uploadsFolderPath); } var fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName); var filePath = Path.Combine(uploadsFolderPath, fileName); using (var stream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(stream); } var photo = new Photo { FileName = fileName }; tvShow.Photoes.Add(photo); await unitOfWork.SaveAsync(); var result = mapper.Map<Photo, PhotoViewModel>(photo); return Ok(result); } } }

这里要简单讲一下. asp.net core 上传文件的文档在这: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads

首先该controller的路由应该遵循web api的规范, 注意controller 的路由地点.

上传单个文件需要使用IFormFile作为Action的参数. 如果上传的是多个文件, 那么应该使用IFormCollection.

这里我做的是单文件上传, 所以使用IFormFile.

随后使用注入的IHostingEnvironment获得wwwroot目录, 我想要把文件上传到wwwroot/uploads下, 判断该目录是否存在, 如果不存在则创建该目录.

为了防黑, 把文件名改成Guid, 后缀名不乱.

然后使用FileStream创建该文件.

后边的内容就是把文件名生存到数据库了.

接下来, 使用Postman来测试这个api.

打开postman, 凭据图示输入:

PhotoViewModel (photo); return Ok(result);}}} 这里要简单讲一下. asp

注意这里的参数的key为file, 这个名字要与action的参数名一致:

PhotoViewModel (photo); return Ok(result);}}} 这里要简单讲一下. asp

send:

PhotoViewModel (photo); return Ok(result);}}} 这里要简单讲一下. asp

很好, 测试通过.

下面为Action添加一些验证:

PhotoViewModel (photo); return Ok(result);}}} 这里要简单讲一下. asp

这就是一些通例的验证, 没有什么特另外, 就不累述了.