日期:2014-05-20 浏览次数:21302 次
System.Net.HttpWebRequest req = System.Net.HttpWebRequest.Create("http://192.168.186.94/fortest.ashx") as System.Net.HttpWebRequest; req.Method = System.Net.WebRequestMethods.Http.Post; System.IO.MemoryStream ms = new System.IO.MemoryStream(); using (System.IO.FileStream fs = new System.IO.FileStream(@"XXX", System.IO.FileMode.Open)) { Int32 by = -1; while ((by = fs.ReadByte()) != -1) { ms.WriteByte((Byte)by); } } Byte[] buffer = ms.ToArray(); req.ContentLength = buffer.Length; req.ContentType = "application/x-www-form-urlencoded"; req.Headers.Add("UserName", "mgsg"); req.Headers.Add("Pwd", "123456789"); req.Headers.Add("FileName", "yiyuan.emr"); req.Headers.Add("Kind", "zhongyi"); req.Headers.Add("Field", "jilu"); System.IO.Stream str = req.GetRequestStream(); str.Write(buffer, 0, buffer.Length); System.Net.HttpWebResponse response = req.GetResponse() as System.Net.HttpWebResponse; if (response.StatusCode == System.Net.HttpStatusCode.OK) { using (System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"))) { Console.Write(sr.ReadToEnd()); } } else Console.WriteLine(response.StatusCode);
HttpPostedFile upFile = up_file.PostedFile;//HttpPostedFile对象,用来读取上传图片的属性 fileLength = upFile.ContentLength;//记录文件的长度 try { if(fileLength==0)//当文件长度为0的时候 { txtMessage.Text = "请选择要上传的文件!"; } else { byte[] fileByte = new byte[fileLength];//用图片的长度来初始化一个字节数组存储临时的图片文件 Stream fileStream = upFile.InputStream;//建立文件流对象 fileStream.Read(fileByte,0,fileLength);//读取图片数据到临时存储体fileByte,0为数据指针位置,fileLength为数据长度 string connString = "Data Source=192.168.1.250;database=image;uid=pwqzc;pwd=cn0088"; SqlConnection conn = new SqlConnection(connString);//初始化数据库连接 string insertStr = "insert into image (image_data,image_content_type,image_description,image_size) values (@image_data,@image_content_type,@image_description,@image_size)"; //插入数据库语句 SqlCommand comm = new SqlCommand(insertStr,conn); comm.Parameters.Add(new SqlParameter("@image_data",SqlDbType.Image));//添加参数 comm.Parameters["@image_data"].Value = fileByte;//给参数赋值 comm.Parameters.Add(new SqlParameter("@image_content_type",SqlDbType.VarChar,50)); comm.Parameters["@image_content_type"].Value = upFile.ContentType;//记录图片类型 comm.Parameters.Add(new SqlParameter("@image_description",SqlDbType.VarChar,50)); comm.Parameters["@image_description"].Value = txtDescription.Text;//把其他的表单数据上传 comm.Parameters.Add(new SqlParameter("@image_size",SqlDbType.Int,4)); comm.Parameters["@image_size"].Value = upFile.ContentLength;//记录图片长度,读取数据的时候使用 conn.Open();//打开数据库连接 comm.ExecuteNonQuery();//添加数据 conn.Close();//关闭数据库 txtMessage.Text = "你已经成功的上传了图片"; } } catch(Exception ex) { txtMessage.Text = ex.Message.ToString(); } } }