如何允许用户拾取多张图片并将图片保存到文件夹并将其名称保存到数据库?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何允许用户拾取多张图片并将图片保存到文件夹并将其名称保存到数据库?相关的知识,希望对你有一定的参考价值。

[Table("Picture")]
public class Picture
{
    [Key]
    public int PictureId { get; set; }

    [Column("Picture")]
    //[Required(ErrorMessage = "Please enter the  location")]
    [StringLength(20, ErrorMessage = "Maximum length allowed is 20")]
    public string PictureURL { get; set; }
    [DisplayName("Appartment")]
    public int AppartmentId { get; set; }
    public virtual Appartment Appartment { get; set; }

}

[Table("Appartment")]
public class Appartment
{
    public Appartment()
    {
        Pictures = new ObservableCollection<Picture>();
    }

    [Key]
    [DisplayName("Appartment id")]
    public int PropertyId { get; set; }

    [Required(ErrorMessage = "Please enter the appartment location")]
    [StringLength(20, ErrorMessage = "Maximum length allowed is 20")]
    public string Location { get; set; }

    [Column(TypeName = "money")]
    public decimal? Price { get; set; }

    [StringLength(100, ErrorMessage = "Maximum length allowed is 100.")]
    [DisplayName("Appartment description")]
    public string ProperyDescription { get; set; }

    [Required(ErrorMessage = "Please fill the area feild.")]
    [StringLength(50)]
    public string Area { get; set; }

    [DisplayName("Floor number")]
    public int FloorNumber { get; set; }

    [DisplayName(" Number of rooms")]
    public int NumberOfRooms { get; set; }

    [DisplayName(" Number of bathrooms")]
    public int NumberOfBathrooms { get; set; }

    [DisplayName(" Number of kitchens")]
    public int NumberOfKitchens { get; set; }

    [StringLength(50, ErrorMessage = "Maximum length allowed is 50.")]

    [DisplayName("type of finishing")]
    public string TypeOfFinshing { get; set; }

    public String AdvertiserId { get; set; }

    public virtual ApplicationUser Advertiser { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ObservableCollection<Picture> Pictures { get; set; }
}

我的控制器:

     public class AppartmentsController : Controller
        {
            private ApplicationDbContext db = new ApplicationDbContext();
     [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Create([Bind(Include = 

"PropertyId,Location,Price,ProperyDescription,Area,FloorNumber,NumberOfRooms,Numb 
       erOfBathrooms ",NumberOfKitchens,TypeOfFinshing,AdvertiserId")] Appartment 
       appartment,  IEnumerable<HttpPostedFileBase> files)
            {
                var pictures = new Picture();
                //int i = -1;
                foreach (var file in files)
                {

                if (file != null && file.ContentLength > 0)
                {

                    string filName= Guid.NewGuid() + 
                    Path.GetFileName(file.FileName);
                    pictures.PictureURL = filName;
                   // pictures.AppartmentId = appartment.PropertyId;


file.SaveAs(Path.Combine(Server.MapPath("/Uploads/"),filName));
                }
            }

            if (ModelState.IsValid)
            {

                appartment.Pictures = new ObservableCollection<Picture>();
                appartment.Pictures.Add(pictures);
                //appartment.AdvertiserId=Get user id   
                db.Appartments.Add(appartment);

                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(appartment);
        }
}

并且用户是aspNetUser,我的问题是在控制器/创建方法中,我希望将图片和公寓ID的名称保存到图片表中,并将当前登录的用户ID保存到公寓表中,请尽快寻求帮助。现在,图片和公寓对象进入create(post)方法,但是问题是如何将每个部分保存到数据库中的表中并将图片保存到文件夹(文件系统)中?

enter code here

我正在使用ASP.NET MVC 5。

答案

** 2天没有回应!。**这里的社区在哪里?

以上是关于如何允许用户拾取多张图片并将图片保存到文件夹并将其名称保存到数据库?的主要内容,如果未能解决你的问题,请参考以下文章

opencv,从图片上用鼠标拾取4个点的坐标,并将其保存在vector<Point>中,有问题,请教!

在这种情况下如何将多张图片上传到远程服务器?循环或重复上传过程?

使用 API 获取 Facebook 头像并将其保存到项目文件夹

如何将过滤器应用于照片并将其保存到数据库 php css javascript

Javascript/Jquery:下载多张图片并保存在本地

将图片保存到共享图片文件夹 android target api 29