返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
StarBlog博客开发笔记

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

曦远 (DealiAxy)约 4 分钟
目录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 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…