1

如何将图像文件分配给 IFormFile 变量?

using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace IFrameTesting.ViewModels
{
    public class ImageModelViewModels
    {
        [Display(Name ="Photo")]
        public IFormFile Photo { get; set; }
        public byte[] ImageData { get; set; }


        [Display(Name = "Converted Photo")]
        public IFormFile ConvertedIFormFilePhoto { get; set; }
    }
}

我已成功转换并存储到服务器端图像。我想从服务器端获取该图像并分配给变量ConvertedIFormFilePhoto如何实现?

假设soultion里面有图片探索路径是wwwroot/images/abc.jpg,我想把这个abc.jpg赋值给变量ConvertedIFormFilePhoto怎么实现呢?

4

1 回答 1

0

假设soultion里面有图片探索路径是wwwroot/images/abc.jpg,我想把这个abc.jpg赋值给变量ConvertedIFormFilePhoto怎么实现呢?

要达到要求,您可以尝试以下代码段:

var filepath = Path.Combine(_env.WebRootPath, @"images\" + "abc.jpg");

using (var stream = System.IO.File.OpenRead($"{filepath}"))
{
    var model = new ImageModelViewModels
    {
        ConvertedIFormFilePhoto = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name))
    };

    // ...
    // code logic here
}
于 2020-05-23T06:42:40.940 回答