在 C# WinForm 应用程序中将图像存储在服务器硬盘上的最佳方法



就我而言,将图像直接存储在SQL DB中并不是最好的方法。特别是,当有很多空间很大时。专家声称,最好将映像名称插入数据库并将映像存储在H-Disk上。

因此,我编写了一些代码,如下所示,将图像保存在用户计算机的H-Disk上:

private void btnSave_Click(object sender, EventArgs e)
{
string imageName = Guid.NewGuid().ToString() + Path.GetExtension(PicBoxCustomer.ImageLocation);
string path = Application.StartupPath + "/Images/";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
PicBoxCustomer.Image.Save(path + imageName);
Customers customers = new Customers()
{
Address = txtAddress.Text,
Email = txtEmail.Text,
FullName = txtName.Text,
Mobile = txtMobile.Text,
CustomerImage = imageName
};
if (customerId == 0)
{
db.CustomerRepository.InsertCustomer(customers);
}
else
{
customers.CustomerID = customerId;
db.CustomerRepository.UpdateCustomer(customers);
}
db.Save();
}

这对于单个用户来说效果很好,但在现实世界中,我的公司有大约 1300 名人员(用户(,我必须找到一种方法,将所有用户 PC 中的图像保存在服务器的硬盘上(而不是在每个用户的计算机上单独保存,以便能够向所有用户显示所有图像(。

我有两个问题:

  1. 如何通过内网将图像从用户的PC传输(上传(到服务器?
  2. 我应该如何更改上述代码以将图像保存在服务器硬盘而不是用户计算机上?

我正在使用具有存储库设计模式的LINQ技术(DataContext(。

你能帮我这样做吗?

您在本主题中提出了多个问题,并且没有说明您的尝试和失败。

对于线索,您应该编写 Web 服务,向客户端提供 API 以将图像上传到服务器并将主题本地存储在服务器中,搜索 WCF 或 WebAPI 以在 C# 中实现 Web 服务。

  • 不要将图像的内容存储在数据库中,因为随着时间的推移,数据库的大小会增加。

相关内容

最新更新