[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.将图片内容通过文件流读取到字节数组
上面是测试代码
时间: 2024-10-12 09:03:00