webservice 上传多张图片

       #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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值