Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 如何将图像文件分配给ASP.net Core中的IFormFile变量?_Asp.net Core_Iformfile - Fatal编程技术网

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
}