#region 测试多图片
[WebMethod (Description="测试多图片")]
public string images()
{
string[] strs = System.IO.Directory.GetFiles("D:\\cs\\");
List<List<byte>> lists = new List<List<byte>>();
foreach (string file in strs)
{
System.IO.FileInfo imgFile = new System.IO.FileInfo(file);
List<byte> list = new List<byte>();
if (imgFile.Extension == ".png" || imgFile.Extension == ".gif" || imgFile.Extension == ".bmp")
{
byte[] imgByte = new byte[imgFile.Length];
System.IO.FileStream imgStream = imgFile.OpenRead();//2.初始化读取图片内容的文件流
imgStream.Read(imgByte, 0, Convert.ToInt32(imgFile.Length));//3.将图片内容通过文件流读取到字节数组
for (int i = 0; i < imgFile.Length; i++)
{
list.Add(imgByte[i]);
}
lists.Add(list);
}
}
string b = ImageService(lists, "456", "44434.png,1234444.png");//4.发送到服务器
return b;
}
#endregion
#region 附件图片上传
[WebMethod(Description = "图片上传")]
public string ImageService(List<List<byte>> strs, string projecID, string filename)
{
byte[] content;
string IsOk = "上传失败,";
string paths = string.Empty;
List<string> listimage = new List<string>();
string imagename = string.Empty;
string[] sArray = Regex.Split(filename, ",", RegexOptions.IgnoreCase);
int indexof = 0;
try
{
string images = string.Empty;
for (int i = 0; i < sArray.Length; i++)
{
imagename = sArray[i].ToString();
listimage.Add(imagename);
}
foreach (List<byte> item in strs)
{
byte[] couby = new byte[item.Count];
couby = item.ToArray();
string pathand = @"D:\WWW\IntelligentPipeline\PreviewFile\Pictures\" + projecID + "\\";
string pathandname = pathand + listimage[indexof];
int index = pathandname.LastIndexOf(".");
if (index == 0)
{
IsOk = "上传失败,";
}
else
{
string extended = string.Empty;
if (index + 1 == pathandname.Length)
{
IsOk = "上传失败,";
}
else
{
extended = pathandname.Substring(index + 1);
if (extended == "jpeg" || extended == "gif" || extended == "jpg" || extended == "bmp" || extended == "png" || extended == "txt")
{
try
{
if (!Directory.Exists(@pathand))//若文件夹不存在则新建文件夹
{
Directory.CreateDirectory(@pathand); //新建文件夹
}
File.WriteAllBytes(pathandname, couby);
images = images + pathandname + ",";
indexof++;
paths = images;
IsOk = paths;
}
catch (Exception ex)
{
IsOk = "上传失败,";
}
}
else
{
IsOk = "上传失败,";
}
}
}
}
}
catch (Exception)
{
IsOk = "上传失败,";
return IsOk;
}
IsOk = IsOk.Substring(0, IsOk.Length - 1);
return IsOk;
}
#endregion
webservice 上传多张图片
最新推荐文章于 2024-05-08 23:39:58 发布