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

时间:2018-09-10 04:08:37

标签: c# winforms linq client-server

就我而言,将图像直接存储在SQL DB中并不是最佳方法。特别是当其中有很多具有巨大空间时。专家认为,最好将图像名称插入数据库并将图像存储在H盘上。

因此,我编写了如下代码,将图像保存在用户计算机的H盘上:

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. 我应如何通过Intranet将图像从用户的PC传输(上传)到服务器?
  2. 如何更改以上代码以将图像保存在服务器硬盘上而不是用户计算机上?

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

你能帮我做到吗?

1 个答案:

答案 0 :(得分:1)

您在此主题中问了多个问题,但未对尝试失败的原因发表任何看法。

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

  • 由于一段时间后数据库的大小增加,因此不将图像的内容存储在数据库中。