Byte // 表示一个 8 位无符号整数。
Count // 获取 System.Collections.Generic.List`1 中包含的元素数。
Length // 获得一个 32 位整数,该整数表示 System.Array 的所有维数中元素的总数。
保存图片时调用服务端的方法,把图片保存进去服务端,所有需要实例化服务器的方法,
客户端只负责把上传的图片传输到服务器,然后在服务器进行处理,并把图片保存在服务器上,然后把图片的链接保存在数据库中。
var length = 0;
byte[][] picture = new byte[lstBytes.Count][];
for (int i = 0; i < lstBytes.Count; i++)
{
picture[i] = lstBytes[i];
length = lstBytes[0].Length;
}
BLL.UC_StaffInformation.UC_StaffInformationClient dbs = new BLL.UC_StaffInformation.UC_StaffInformationClient();
dbs.aClick_AddStaff(picture);
public int aClick_AddStaff(i byte[][] bytepicture) {
string picture = IOSwitchPath(bytepicture);
SqlParameter[] sqlparameter = {
new SqlParameter("@type",SqlDbType.Char)
,new SqlParameter("@picture",SqlDbType.Char)
};
sqlparameter[0].Value = "Click_AddStaff";
sqlparameter[1].Value = picture;
int Count = dalMethod.UpdateData("UC_StaffInformation", sqlparameter);
return Count;
}
调用方法转换一下字节流
我们主要是把图片保存进去服务器的文件夹里面,
所以我们需要先使用System.AppDomain.CurrentDomain.BaseDirectory来
获取一下基目录,然后判断文件夹是否存在,
如果不存在我们需要创建一个新的文件夹
然后还需要跟图片重命名,
我们使用的是时间加上随机数来创建新的文件名称,
之后使用for循环来遍历图片的数量,然后需要调用IO流中的函数
FileInfo作为文件路径的包装
然后调用函数FileStream以文件为主的 System.IO.Stream,既支持同步读写操作
,也支持异步读写操作。
然后调用OpenWrite来写入文件
使用完成了使用Close来关闭文件流
private string IOSwitchPath(byte[][] byte_picture)
{
try
{
//获取基目录,它由程序集冲突解决程序用来探测程序集。
string strSystemPath = System.AppDomain.CurrentDomain.BaseDirectory + "image\\";
if (!System.IO.Directory.Exists(strSystemPath))
{
System.IO.Directory.CreateDirectory(strSystemPath);
}
//拼接日期
string strGuid = DateTime.Now.ToString("yyyyMMddhhmmssss") + Guid.NewGuid();
string strPicturePath = "";
//遍历二进制的数组的数组
for (int i = 0; i < byte_picture.Length; i++)
{
string strRiQiWenJian = strGuid + ".png";
//拼接路径
strSystemPath = strSystemPath + strRiQiWenJian;
FileInfo fi = new System.IO.FileInfo(strSystemPath);
FileStream fs;
fs = fi.OpenWrite();
//将字节块写入文件流。(数组,开始字节索引,长度)
fs.Write(byte_picture[i], 0, byte_picture[i].Length);
//关闭当前流并释放与之关联的所有资源
fs.Close();
strPicturePath = strRiQiWenJian;
}
return strPicturePath;
}
catch
{
return null;
}
}
~~~~~~~~~~~~~~~~~~~~~