简体   繁体   English

如何在 ASP.NET CORE 中将文件路径转换为 ​​IFormFIle?

[英]How to convert a file path to IFormFIle in ASP.NET CORE?

How do i get an IFormFile Instance using a file path?.如何使用文件路径获取 IFormFile 实例? The file, in this case is an Image stored in the wwwroot folder.在这种情况下,该文件是存储在wwwroot文件夹中的图像

I'll give you a little bit of context of what i am trying to achieve:我会给你一些我想要实现的目标的背景:

I want to update a product so in order to do that i have to display the product information on the view, including it's file name next to the <input type="file" class="form-control-file" asp-for="File"> .我想更新一个产品,所以为了做到这一点,我必须在视图上显示产品信息,包括<input type="file" class="form-control-file" asp-for="File">旁边的文件名<input type="file" class="form-control-file" asp-for="File"> So the user knows,it has already an image, and of course i won't have problem doing the HTTP Post Request because no file was selected (In case the user doesn't want to update the image).所以用户知道,它已经有一个图像,当然我不会在执行 HTTP Post 请求时遇到问题,因为没有选择文件(以防用户不想更新图像)。 The problem is that product class doesn't have an IFormFile property, it only has it's image name, and the view model is responsible of having the IForm File.问题是产品类没有 IFormFile 属性,它只有图像名称,而视图模型负责拥有 IForm 文件。 So again how do i convert the image name or path into a IFormFile before sending the productViewModel as a parameter to the View?那么,在将 productViewModel 作为参数发送到 View 之前,如何将图像名称或路径转换为 ​​IFormFile ?

Here's my view,view model and my product class:这是我的视图、视图模型和我的产品类:
Update.cshtml

<form enctype="multipart/form-data" method="post" asp-controller="Product" asp-action="Update">
    <div class="form-group">
        <label asp-for="Name"></label>
        <input type="text" class="form-control" asp-for="Name" placeholder="Cereal, Ice cream, Tuna, etc...">
        <span class="text-danger" asp-validation-for="Name"></span>
    </div>
    <div class="form-group">
        <label asp-for="File"></label>
        <input type="file" class="form-control-file" asp-for="File">
    </div>
    <button class="btn btn-success">Save Changes</button>
</form>

ProductViewModel.cshtml

public class ProductViewModel
{
    public string Name { get; set; }
    public IFormFile File { get; set; }
}

Product.cshtml

public class Product
{
    public string Name { get; set; }
    public string ImageName { get; set; }
}

Here is a demo to change path to IFormFile in Action:这是一个在操作中更改 IFormFile 路径的演示:

Controller:控制器:

public IActionResult GetFile() {
            Product product = new Product { Name = "product1", ImageName = "red.PNG" };
            string path = "./wwwroot/images/" + product.ImageName;
            using (var stream = System.IO.File.OpenRead(path))
            {
                ProductViewModel productViewModel = new ProductViewModel
                {
                    Name = product.Name,

                    File = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name))
                };
                return Ok();
            }
            
        }

Result:结果: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在asp.net MVC Core的IFormFile中转换byte []? - How to convert byte[] in IFormFile in asp.net MVC Core? 使用IFormFile拒绝ASP.Net对路径的核心访问 - ASP.Net Core Access to the Path is Denied with IFormFile 如何在 asp.net 核心 1.0 c# 中将字符串(实际上是位图)转换为 IFormFile - How can I convert String (which is actually Bitmap) to IFormFile in asp.net core 1.0 c# 如何在 ASP.NET Core 中为单元/集成测试模拟 IFormFile? - How to mock an IFormFile for a unit/integration test in ASP.NET Core? 如何在 iFormFile、ASP.net Core 中设置默认图像 - How to set default image in iFormFile, ASP.net Core 使用 Asp.NET Core 3.1 框架将文件上传到服务器时如何使用 IFormFile 作为属性? - How to use IFormFile as property when uploading file to a server using Asp.NET Core 3.1 framework? ASP.NET Core MVC:上传的文件很大时,IFormFile 返回 Null - ASP.NET Core MVC: IFormFile return Null when uploaded file is large 是否有必要Dispose对象包含IFormFile(二进制文件) asp.net core 2 - is it necessary to Dispose objects contains IFormFile (binary file) asp.net core 2 将IFormFile转换为ASP Core中的图像 - Convert IFormFile to Image in Asp Core 在ASP.Net Core中验证IFormFile的图像类型 - Validate Image type for IFormFile in ASP.Net Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM