首页 > 解决方案 > 为什么我的文件没有保存到 ASP.NET Core 应用程序中的指定目录?

问题描述

我正在尝试将文件保存到我的 ASp.NET Core 应用程序中的本地目录,虽然还有其他更好的方法可以做到这一点,但这是用户请求的方式。因此,我在控制器中设置了文件上传,如下所示:

家庭控制器.cs

[HttpPost]
public IActionResult Create(CreateProductViewModel model)
{
    if (ModelState.IsValid && model !=null)
    {
        try
        {
            //Save the Product
            var product = new Products
            {
                Id = model.Products.Id,
                Title = model.Products.Title,
            };
            //Insert the product and get the newly created Id
            _productsService.InsertProduct(product);
            int newId = product.Id;

            //Process the Images
            if (model.Products.ProductImages != null)
            {
                try
                {                            
                    var root = _env.WebRootPath;
                    var images = model.Products.ProductImages;
                    var path = Path.Combine(root, "images", "products", newId.ToString());                            

                    foreach (var item in images) 
                    {
                        using (var stream = System.IO.File.Create(path))
                        {                                    
                            Stream inStream = item.OpenReadStream();
                            using Image image = Image.Load(inStream);
                            int width = 350;
                            int height = 0;
                            var clone = image.Clone(i => i.Resize(width, height));
                            clone.SaveAsJpeg(stream);                                
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
        catch (Exception ex) {
            Console.WriteLine(ex.Message);
        };
    }
    return RedirectToAction("Index", "Products");
}

这是代表此数据的模型

using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;

namespace MyProject.Data
{
    public class Products : BaseEntity
    {           
        public string Title { get; set; }  

        [NotMapped]
        public IFormFileCollection ProductImages { get; set; }
    }
}

有几件事情发生,过程需要如下

IE

但是,它并不完全像那样工作。图像未保存,应用程序正在创建一个以产品 ID 作为名称的空文件(0 字节):

我做错了什么,它没有正确创建产品 ID,为什么不将我的文件保存到该目录中?我觉得我错过了一步。

标签: c#asp.net-core

解决方案


path是错的。您将所有 iamges 以相同的名称保存到同一目录中。因为线using (var stream = System.IO.File.Create(path))

图像高度为 0。你也必须改变它。

尝试这个。

[HttpPost]
public IActionResult Create(CreateProductViewModel model)
{
    if (ModelState.IsValid && model !=null)
    {
        try
        {
            //Save the Product
            var product = new Products
            {
                Id = model.Products.Id,
                Title = model.Products.Title,
            };
            //Insert the product and get the newly created Id
            _productsService.InsertProduct(product);
            int newId = product.Id;

            //Process the Images
            if (model.Products.ProductImages != null)
            {
                try
                {                            
                    var root = _env.WebRootPath;
                    var images = model.Products.ProductImages;
                    var saveDirectory = Path.Combine(root, "images", "products", newId.ToString());                            

                    var iterator = 0;
                    foreach (var item in images) 
                    {
                        using (var stream = System.IO.File.Create(Path.Combine(saveDirectory, $"image{iterator++}.jpg")))
                        {                                    
                            Stream inStream = item.OpenReadStream();
                            using Image image = Image.Load(inStream);
                            int width = 350;
                            int height = WHY TIS IS ZERO?;
                            var clone = image.Clone(i => i.Resize(width, height));
                            clone.SaveAsJpeg(stream);                                
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
        catch (Exception ex) {
            Console.WriteLine(ex.Message);
        };
    }
    return RedirectToAction("Index", "Products");
}

推荐阅读