【问题标题】:How to assign image file to IFormFile Variable in ASP.net Core?如何将图像文件分配给 ASP.net Core 中的 IFormFile 变量?
【发布时间】:2020-05-22 12:28:34
【问题描述】:

如何将图像文件分配给 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怎么实现?

【问题讨论】:

    标签: asp.net-core iformfile


    【解决方案1】:

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

    要达到要求,可以试试下面的代码sn -p:

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-04
      • 1970-01-01
      • 2014-07-26
      • 2020-04-28
      • 2022-01-06
      • 2014-04-23
      相关资源
      最近更新 更多