码迷,mamicode.com
首页 > Web开发 > 详细

webservice 以流的形式上传图片

时间:2015-10-09 11:56:21      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

[System.Web.Services.Protocols.SoapHeader("myHeader")]
        [WebMethod(Description = "上传文件到远程服务器.")]
        public string UploadFile(byte[] fileBytes, string type,string so_no)
        {
            try
            {
                string fileName = string.Empty;
                if (type.ToLower() == "jpg" || type.ToLower() == "gif" || type.ToLower() == "bmp" || type.ToLower() == "png" || type.ToLower() == "jpeg")
                {
                     fileName = Guid.NewGuid().ToString() + "." + type;
                }
                MemoryStream memoryStream = new MemoryStream(fileBytes); //1.定义并实例化一个内存流,以存放提交上来的字节数组。  
                string strName = @"D:\程序开发\三星\WebApplication1\WebApplication1\img\" + fileName;
                FileStream fileUpload = new FileStream(strName, FileMode.Create); ///2.定义实际文件对象,保存上载的文件。  
                memoryStream.WriteTo(fileUpload); ///3.把内存流里的数据写入物理文件  
                memoryStream.Close();
                fileUpload.Close();
                fileUpload = null;
                memoryStream = null;
                add(strName, fileName, type,so_no);
                return "文件已经上传成功";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        private void add(string path, string filename, string type,string so_no)
        {
            FileStream fs = File.OpenRead(path);
            byte[] content = new byte[fs.Length];
            fs.Read(content, 0, content.Length);
            fs.Close();
            string path1 = @"img\" + filename;
            string sql = @"insert into image(id,so_no,filename,address,time) values(seq_temp.NEXTVAL,:so_no,:filename,:address,sysdate )";
            OracleConnection connection = new OracleConnection();
            connection.ConnectionString = "Data Source=SAMSUNG;User ID=samsung;Password=samsung";//此处设置链接字符串
            OracleCommand command = new OracleCommand(sql, connection);

            command.Parameters.Add(":so_no", OracleType.VarChar, 100).Value = so_no;
            command.Parameters.Add(":filename", OracleType.VarChar, 100).Value = filename;
            command.Parameters.Add(":address", OracleType.VarChar, 200).Value = path1;
           
            connection.Open();

            int str1 = command.ExecuteNonQuery();
            connection.Close();
            command.Dispose();
        }

上面是webservice代码

 FileInfo imgFile = new FileInfo(@"D:\test1.jpg");
            byte[] imgByte = new byte[imgFile.Length];//1.初始化用于存放图片的字节数组  
            System.IO.FileStream imgStream = imgFile.OpenRead();//2.初始化读取图片内容的文件流  
            imgStream.Read(imgByte, 0, Convert.ToInt32(imgFile.Length));//3.将图片内容通过文件流读取到字节数组  

上面是测试代码

webservice 以流的形式上传图片

标签:

原文地址:http://www.cnblogs.com/110abcd/p/4863298.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!