Asp.net core 如何将图像文件分配给ASP.net Core中的IFormFile变量?
如何将图像文件分配给文件变量Asp.net core 如何将图像文件分配给ASP.net Core中的IFormFile变量?,asp.net-core,iformfile,Asp.net Core,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 Image
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 Explore路径是wwwroot/images/abc.jpg,我想将这个abc.jpg分配给变量ConvertedIFormFilePhoto如何实现它
假设soultion Explore路径是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
}
假设soultion Explore路径是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
}