【问题标题】:How to add image path to database + associate images with albums?如何将图像路径添加到数据库+将图像与相册关联?
【发布时间】:2019-05-10 10:45:28
【问题描述】:

我正在制作一个包含相册和图片的网站,只是为了学习如何使用图片。我可以更改图像名称并将它们上传到图像文件夹。

我的问题有两个:

(1) 不知道如何将图片路径添加到数据库
(2) 我不知道如何为每个相册添加图片列表

我花了很多时间试图找到没有结果的答案。

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

        public int AlbumId { get; set; }
        [ForeignKey("AlbumId")]
        public virtual Album Album { get; set; }

        public string ImagePath { get; set; }
    }
 public class Album
    {
        public int Id { get; set; }
        public string AlbumTitle { get; set; }

        public List<Image> Image { get; set; }
    }
 public async Task<IActionResult> UploadImage(int id, Album album)
        {
            if (id != album.Id)
            {
                return NotFound();
            }

            var newFileName = string.Empty;

            if (HttpContext.Request.Form.Files != null)
            {
                var fileName = string.Empty;
                string PathDB = string.Empty;

                var files = HttpContext.Request.Form.Files;

                foreach (var file in files)
                {
                    if (file.Length > 0)
                    {
                        fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');

                        var myUniqueFileName = Convert.ToString(Guid.NewGuid());

                        var FileExtension = Path.GetExtension(fileName);

                        newFileName = myUniqueFileName + FileExtension;

                        fileName = Path.Combine(_hostingEnvironment.WebRootPath, "images") + $@"\{newFileName}";
                        PathDB = "images/" + newFileName;

                        using (FileStream fs = System.IO.File.Create(fileName))
                        {
                            file.CopyTo(fs);
                            fs.Flush();
                        }
                    }
                }
            }

            _db.Update(album);
            await _db.SaveChangesAsync();

            return View("Index");

        }

上传图片的相册 ID 传入该方法。我需要将这张专辑 ID 以及图像路径(我还需要图像名称吗?)到数据库中。我该怎么做?

还有,是

public List&lt;Image&gt; Image { get; set; }

在相册类中存储图像的正确方法?

【问题讨论】:

  • 在您的数据库中,您需要定义类型为 varbinary(max) 的列,而在 c# 中,为了将图像传递到数据库,您需要将读取的图像转换为字节数组 []
  • 您需要更新或创建 album.id 。将文件名和album.Id保存在db.Image中

标签: asp.net asp.net-core


【解决方案1】:

你可以试试这个文件路径

string filepath=Path.GetFullPath(file)

对于文件名

string name=Path.GetFileName(file)
imagepath=filepath+name;

并将其插入数据库

 {
                    string sql = "insert into tablename values(@id,@AlbumId,@imagepath)";
                    using (SqlCommand cmd = new SqlCommand(sql, con))
 {     
 cmd.Parameters.AddWithValue("@id", id);
cmd.Parameters.AddWithValue("@AlbumId", albumid);
 cmd.Parameters.AddWithValue("@Imagepath",imagepath);
 con.Open();
 cmd.ExecuteScalar();
 con.Close();
}
}

【讨论】:

    【解决方案2】:

    @m1547,

    @marcelo's answer 应该让你朝着正确的方向前进,实现方面。除此之外,还有几点需要注意:

    • 将图像存储在文件夹中时,将其重命名为 GUID 而不是原来的 图像的文件名。这将允许您存储两个原始图像文件名相同的图像。 t
    • 更新您的 SQL 架构以包含以下列:
      • 图像的 GUID(您的程序设置的),
      • 原始文件名(在屏幕上向用户显示图像时很有用),
      • 文件夹路径(物理文件在服务器中的位置),
      • 相册路径(在您的相册只是虚拟相册的情况下,否则在您创建代表相册的物理文件夹的情况下,您的文件夹路径应该已经解决了这个问题)

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-12
      • 1970-01-01
      • 2021-02-20
      • 1970-01-01
      相关资源
      最近更新 更多