StarBlog Blog Development Notes: Image Upload Feature
Backend implementation, frontend will be covered later You can test it in Swagger first ImageSharp component documentation: https://docs.sixlabors.com/articles/imagesharp/gettingstarted.html service private string GetPhotoFilePath(Photo photo) { return Path.Combine(_environment
目录2 个章节
Backend implementation, frontend will be covered later
You can test it in Swagger first
ImageSharp component documentation: https://docs.sixlabors.com/articles/imagesharp/gettingstarted.html
service
private string GetPhotoFilePath(Photo photo) {
return Path.Combine(_environment.WebRootPath, "media", photo.FilePath);
}
private Photo BuildPhotoData(Photo photo) {
var savePath = GetPhotoFilePath(photo);
using (var img = Image.Load(savePath)) {
photo.Height = img.Height;
photo.Width = img.Width;
}
return photo;
}
public Photo Add(PhotoCreationDto dto, IFormFile photoFile) {
var photoId = GuidUtils.GuidTo16String();
var photo = new Photo {
Id = photoId,
Title = dto.Title,
CreateTime = DateTime.Now,
Location = dto.Location,
FilePath = Path.Combine("photography", $"{photoId}.jpg")
};
var savePath = GetPhotoFilePath(photo);
using (var fs = new FileStream(savePath, FileMode.Create)) {
photoFile.CopyTo(fs);
}
photo = BuildPhotoData(photo);
return _photoRepo.Insert(photo);
}
controller
[HttpPost]
public ApiResponse<Photo> Add([FromForm] string title, IFormFile file) {
var photo = _photoService.Add(title, file);
return !ModelState.IsValid
? ApiResponse.BadRequest(Response, ModelState)
: new ApiResponse<Photo>(photo);
}
_EOF_
StarBlog Blog Development Notes: Image Upload Feature
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
StarBlog博客开发笔记(19):Markdown渲染方案探索
前言 笔者认为,一个博客网站,最核心的是阅读体验。 在开发StarBlog的过程中,最耗时的恰恰也是文章的展示部分功能。 最开始还没研究出来如何很好的使用后端渲染,所以只能先用Editor.md组件做前端渲染,过渡一下。前端渲染我是不满意的,因为性能较差,页面加载出来还会闪一下,有割裂感,影响体验。 现在我已经做出了比较完善的后端渲染方案,前端渲染就可以直接退休了。本文介绍StarBlog博客开发
2022-10-28 · StarBlog博客开发笔记 · 约 1 分钟
StarBlog博客开发笔记(6):页面开发之博客文章列表
系列文章 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客? 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目 基于.NetCore开发博客项目 StarBlog - (3) 模型设计 基于.NetCore开发博客项目 StarBlog - (4) markdown博客批量导入 基于.NetCore开发博客项目 StarBl
2023-05-07 · StarBlog博客开发笔记 · 约 1 分钟
StarBlog博客开发笔记(14):实现主题切换功能
系列文章 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客? 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目 基于.NetCore开发博客项目 StarBlog - (3) 模型设计 基于.NetCore开发博客项目 StarBlog - (4) markdown博客批量导入 基于.NetCore开发博客项目 StarBl
2022-07-03 · StarBlog博客开发笔记 · 约 1 分钟
基于.NetCore开发博客项目 StarBlog - (28) 开发友情链接相关接口
之前介绍的友情链接功能,只实现了友情链接的展示和管理接口。 还缺失友情链接申请、审核管理、通知,现在把这块功能补全。 Model 什么的之前那篇文章都有,本文直接补全逻辑代码~
2023-06-02 · StarBlog博客开发笔记 · 约 1 分钟
讨论
共 0 条加载评论中…