繁体   English   中英

如何在MVC asp.net core +在SQL Server上更新中上传文件?

[英]How to upload files in MVC asp.net core + update on sql server?

我想在MVC点网核心2中上传文件,我是学生,并且是该技术的新手(我在这里阅读了一些问答,但对该问题没有答案)。

我的产品模型有添加操作和视图。 在视图中,存在一个用于创建具有某些属性的产品的表单。 我还想添加一个上传图片的选项-单击添加按钮后,需要先使用实体​​框架代码将产品保存在数据库中。

我不使用ViewModel(并且我不想要使用它的解决方案),但是也许以后我将要为Image实体创建类。 现在不行(对我来说太多了)。

请帮我怎么做。 这是代码。

型号-产品:

public class Product
    {
        [Key]
        public Guid Id { get; set; }
        [Required]
        [StringLength(50, MinimumLength = 3)]
        public string Name { get; set; }
        [Required]
        public string Description { get; set; }
        [Required]
        public string Category { get; set; }
        [Required]
        public string Gender { get; set; }
        [Required]
        public string Age { get; set; }
        [Required]
        public bool ConditionIsNew { get; set; }
    }

控制器-ProductController

public class ProductController : Controller
    {
            [HttpGet]
            public IActionResult Add()
            {
                return View(new Product());
            }

            [HttpPost]
            public IActionResult Add(Product p)
            {
                if (ModelState.IsValid)
                {
                    ProductDal dal = new ProductDal();
                    dal.Products.Add(p);
                    dal.SaveChanges();
                    return View("Show", p);
                }
                return View(p);
            }
    }

查看-添加(剃刀语法,标签助手)

@model MyProjectTest.Models.Product
    @{
        Layout = "~/Shared/_Layout.cshtml";
    }
<h1>Submit</h1>
    <h2>Add product</h2>
    <form asp-controller="Product" asp-action="Add" method="post">
            <label asp-for="Name"></label> <input asp-for="Name" /> <span asp-validation-for="Name"></span>
            <br />
            <label asp-for="Description"></label> <input asp-for="Description" /> <span asp-validation-for="Description"></span>
            <br />
            <label asp-for="Category"></label> <input asp-for="Category" /> <span asp-validation-for="Category"></span>
            <br />
            <label asp-for="Gender"></label> <input asp-for="Gender" /> <span asp-validation-for="Gender"></span>
            <br />
            <label asp-for="Age"></label> <input asp-for="Age" /> <span asp-validation-for="Age"></span>
            <br />
            <label asp-for="ConditionIsNew"></label> <input asp-for="ConditionIsNew" /> <span asp-validation-for="ConditionIsNew"></span>
            <br />
            <input id="SubmitProduct" type="submit" value="Enter" />
        </form>

再说一次,我没有ViewModel,也不想使用它。

我也想知道如何将图像更新到数据库

谢谢

https://www.w3schools.com/tags/att_input_type_file.asp

https://docs.microsoft.com/zh-cn/aspnet/core/razor-pages/upload-files?view=aspnetcore-2.2

您需要知道的一件事。 输入将包含上载文件的二进制表示形式,因此如果您想以某种方式使用它,则需要记住对它进行反序列化/序列化。

HTML层:*输入type =“ file” name =“ fileABC” />

控制器层:带有HttpPostedFileBase类型的参数并命名为fileABC的方法html post(在这种情况下,必须与模型的参数分开)产品模型)

对于SQL:使用blob,文件流...

暂无
暂无

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

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