webservice上传图片

        public string servicepath = "";
        public StringBuilder pathstr = new StringBuilder();
        private void btnExplo_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textImagePath.Text = openFileDialog1.FileName;
            }
        }

        private void btnUpPic_Click(object sender, EventArgs e)
        {
            string name = CommonClass.FileManage.GenerateId();
            string filepath = textImagePath.Text;

            if (File.Exists(filepath))
            {
                if (CommonClass.FileManage.CheckImgFormat(@filepath))
                {
                    FileInfo fileInfo = new FileInfo(filepath);
                    if (fileInfo.Length < 3145728)
                    {
                        BackGroundManage.ServiceReference1.SecurityHeader header = new BackGroundManage.ServiceReference1.SecurityHeader();
                        header.SecurityKey = CommonClass.XmlManage.ReadConNode("upload", "SecurityKey");
                        BackGroundManage.ServiceReference1.UpDownServiceSoapClient up = new BackGroundManage.ServiceReference1.UpDownServiceSoapClient();

                        pathstr.Append(servicepath);
                        pathstr.Append(DateTime.Now.Year.ToString());
                        if (DateTime.Now.Month < 10)
                        {
                            pathstr.Append("0");
                        }
                        pathstr.Append(DateTime.Now.Month.ToString());
                        pathstr.Append("\\");
                        if (DateTime.Now.Day < 10)
                        {
                            pathstr.Append("0");
                        }
                        pathstr.Append(DateTime.Now.Day.ToString());
                        pathstr.Append("_");
                        pathstr.Append(name);
                        pathstr.Append(".");
                        pathstr.Append(filepath.Substring(filepath.LastIndexOf('.') + 1));

                        //数据库保存时名称为"Images"+"\\"+"pathstr.ToString()

                        bool upf = up.Up(header, CommonClass.FileManage.FileToByte(@filepath), pathstr.ToString());
                        if (upf)
                        {
                            MessageBox.Show("成功");
                        }
                        else
                        {
                            MessageBox.Show("失败");
                        }
                    }
                    else
                    {
                        MessageBox.Show("上传图片大小超过限制!\n只允许小于3M的文件!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
                else
                {
                    MessageBox.Show("上传图片格式不正确!\n只允许上传.gif/.jpg/.png的文件!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                MessageBox.Show("文件不存在\n请重新选择文件!!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            }
        }
posted @ 2011-07-11 16:24  小羽毛的天空  阅读(955)  评论(0编辑  收藏  举报