Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.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

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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.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
C# 为什么当我选择多个文件时,IFormfile只将第一个文件转换为字节?_C#_Asp.net Core - Fatal编程技术网

C# 为什么当我选择多个文件时,IFormfile只将第一个文件转换为字节?

C# 为什么当我选择多个文件时,IFormfile只将第一个文件转换为字节?,c#,asp.net-core,C#,Asp.net Core,使用Asp.net Core 3.0。 我试图上传多个图像到一个数据库对一个产品转换成字节的形式,但它只是迭代通过第一个选定的文件,并将其转换为字节,并只保存第一个文件到数据库,而不是迭代通过每个文件,并保存在数据库中的每个文件。在调试时,它显示它遍历每个项目,但仍然只保存第一个项目。没有错,但我还是不明白。这是数据库截图 代码如下: 控制器代码 [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult&g

使用Asp.net Core 3.0。

我试图上传多个图像到一个数据库对一个产品转换成字节的形式,但它只是迭代通过第一个选定的文件,并将其转换为字节,并只保存第一个文件到数据库,而不是迭代通过每个文件,并保存在数据库中的每个文件。在调试时,它显示它遍历每个项目,但仍然只保存第一个项目。没有错,但我还是不明白。这是数据库截图

代码如下:

控制器代码

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(ProductViewModel model, List<IFormFile> Images)
{
    try
    {
        var chheck = _context.Products.Include(x => x.Images);
        if (ModelState.IsValid)
        {
            var enter = new Product()
            {
                ProductCode = model.ProductCode,
                Price = model.Price,
                Title = model.Title,
                Description = model.Description,
                Type = model.Type,
                CatagoryId = model.CatagoryId
            };

            //   var prod = _context.Products.Where(x => x.ProductID == Product.ProductID.Value );

            if (model.Images != null && model.Images.FileName != "" && model.Images.FileName != null)
            {
                IFormFile file = model.Images;

                foreach (var imag in Images)
                {
                    if (imag.Length > 0)
                    {
                        using (var stream = new MemoryStream())
                        {
                            Image img = new Image();
                            {
                                await file.OpenReadStream().CopyToAsync(stream);

                                img.FileName = file.FileName;
                                img.ContentType = file.ContentType;
                                img.ProductCode = model.ProductCode;

                                img.Pic = stream.ToArray();
                                stream.Close();
                                stream.Dispose();
                            };
                            _context.Images.Add(img);
                            await _context.SaveChangesAsync();
                        }
                    }
                }
            }

在您的操作实现过程中,似乎有一些事情没有完成。首先,让我们看看它的签名:

Task<IActionResult> Create(ProductViewModel model, List<IFormFile> Images)
因此,您需要检查视图模型中名为
Images
的单个
ifformfile
对象。请注意,它被称为复数“图像”,但只是一个
格式文件
。因此,它也只能包含一个文件

foreach (var imag in Images)
{
    if (imag.Length > 0)
然后循环浏览
图像
,即
文件
对象列表。这将是您获取所有要处理的单个文件的地方。因此,每个
imag
都是上传的文件之一

但是,在循环中实际处理的是
文件
,即
model.Images
,即单个
文件
对象。因此,您应该修改代码以实际处理循环变量
imag

using (var stream = new MemoryStream())
{
    await imag.OpenReadStream().CopyToAsync(stream);

    Image img = new Image();    
    img.FileName = imag.FileName;
    img.ContentType = imag.ContentType;
    img.ProductCode = model.ProductCode;   
    img.Pic = stream.ToArray();

    _context.Images.Add(img);
    await _context.SaveChangesAsync();
}

在循环内部,您没有使用
imag
执行任何操作。每次迭代只将相同的
文件加载到流中。@evaluator您的意思是它应该是
imag.Pic=stream.ToArray()
等待文件.OpenReadStream().CopyToAsync(流)始终是同一个文件。你为什么使用
文件而不是
imag
?@ifrahnayer太棒了!不客气:)请记住,如果问题解决了,请将问题标记为已解决。
foreach (var imag in Images)
{
    if (imag.Length > 0)
using (var stream = new MemoryStream())
{
    await imag.OpenReadStream().CopyToAsync(stream);

    Image img = new Image();    
    img.FileName = imag.FileName;
    img.ContentType = imag.ContentType;
    img.ProductCode = model.ProductCode;   
    img.Pic = stream.ToArray();

    _context.Images.Add(img);
    await _context.SaveChangesAsync();
}