c# - 为什么我的文件没有保存到 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; }
}
}
有几件事情发生,过程需要如下
- 产品已创建
- 新创建的产品id被抓取
- 图片路径是使用新的产品 ID 创建的
- 所有图像都经过迭代、处理并保存到该新目录中。
IE
wwwroot/images/products/54/imagefile01.jpg
wwwroot/images/products/54/imagefile02.jpg
wwwroot/images/products/54/imagefile03.jpg
但是,它并不完全像那样工作。图像未保存,应用程序正在创建一个以产品 ID 作为名称的空文件(0 字节):
wwwroot/images/products/54
我做错了什么,它没有正确创建产品 ID,为什么不将我的文件保存到该目录中?我觉得我错过了一步。
解决方案
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");
}
推荐阅读
- python - 具有 groupby 和聚合操作的简单并行处理 pandas Dataframe 块
- angular - Angular 7+ 中的共享动画
- c# - 如何在军刀汽车价格空气交换肥皂 api 请求中发送段号
- python - 在每次迭代时在带有标识列的 pandas 上迭代地创建一个数据框
- flutter - Flutter App 介绍与用户交互
- javascript - Fancybox 导致我的另一个
你好,我想知道如何将我的fancybox 仅应用于一个标签而不是我的所有标签,因为它们会搞砸。有没有办法停止代码,所以它不会弄乱页面上的其余 a 标签。或者也许将fancybox设置为仅对一个标签ID起作用。
这是我在页面上的 fancybox 脚本:
$(document).ready(function()
- selenium - 当我们使用 jenkins 执行构建时,testng-failed.xml 没有创建
- q# - Q#中为什么用返回类型Unit来描述没有返回值的操作;相对于无效或无?
- jquery - 为什么我看到 webpack 模块 7 不是一个函数?
- r - 根据最长行重塑r中的数据框