简体   繁体   English

在 Asp.Net core 中上传图片失败

[英]Failed to upload an image in Asp.Net core

I am trying to a single image upload.我正在尝试上传单个图像。 but the problem is when I upload an image, I find null (i debugged).但问题是当我上传图片时,我发现 null (我已调试)。 Here is my code:这是我的代码:

  [HttpPost]
        public async   Task <IActionResult> Create(Image products, IFormFile image)
        {
           
            if (ModelState.IsValid)
            {
                if (image != null)
                {
                    var name1 = Path.Combine(_he.WebRootPath + "/Images", Path.GetFileName(image.FileName));
                    var stream2 = new FileStream(name1, FileMode.Create);
                   await image.CopyToAsync(stream2);

                    products.ImageC = "Images/" + image.FileName;
                }


                if (image == null)
                {
                    products.ImageC = "Images/NoImageFound.jpg";
                }



                _db.Images.Add(products);
                 _db.SaveChanges();
                return RedirectToAction(nameof(Index));
            }
            return View(products);
        }


_Create.cshtml _Create.cshtml

<form asp-action="Create" method="post"  enctype="multipart/form-data">
    <div class="p-4 rounded border">
        <div asp-validation-summary="ModelOnly" class="text-danger">

        </div>

        <div class="form-group row">

            <div class="col-5">
                <p class="text-bold mb-2">Image</p>
                <input asp-for="ImageC" class="" type="file" />
            </div>
           
        </div>


        <div class="form-group">
            <input type="submit" class="btn btn-danger form-control" value="Submit" />

        </div>
    </div>
</form>


//Image.cs (Model)

 public class Image
    {
        public int Id { get; set; }
        public string ImageC { get; set; }
    }

When I type the submit button then:-当我键入提交按钮时:-

在此处输入图像描述

then I debugged my code and found this issue:-然后我调试了我的代码并发现了这个问题:-

在此处输入图像描述

I don't understand why I found this null reference?我不明白为什么我找到这个 null 参考? how I will upload my image?我将如何上传我的图片? I am still a beginner, please help.我还是一个初学者,请帮助。

I assume the model passed to the view in the HttpGet action is an empty instance of the Image class and that's the model returned by the HttpPost action.我假设在 HttpGet 操作中传递给视图的 model 是 Image class 的空实例,这就是 HttpPost 操作返回的 model。

So we could simply declare a IFormFile property in the class itself and let the engine fill the property with the data from the type="file" control.所以我们可以简单地在 class 本身中声明一个 IFormFile 属性,然后让引擎用type="file"控件中的数据填充该属性。

At this point then there is no need to have a parameter of type IFormFile in the HttpPost Create method.此时,HttpPost Create 方法中不需要 IFormFile 类型的参数。

public class Image
{
    public int Id { get; set; }
    public IFormFile ImageC { get; set; }
}

Then all other changes required in the HttpPost Create method are a consequence of this change.那么 HttpPost Create 方法中所需的所有其他更改都是此更改的结果。

However, it seems like you have the same model for the view and the database.但是,您的视图和数据库似乎具有相同的 model。 In this simple case you can go ahead and forget about the concept of ViewModels but it is best, for your future efforts to consider this separation between models and viewmodels.在这个简单的情况下,您可以提前 go 并忘记 ViewModels 的概念,但最好是为了您未来的努力考虑模型和视图模型之间的这种分离。 The UI should know nothing about the models used to interact with the database. UI 应该对用于与数据库交互的模型一无所知。
More information about this topic could be found here ASP.NET MVC Model vs ViewModel可以在此处找到有关此主题的更多信息ASP.NET MVC Model 与 ViewModel

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM