asp.net上传图片
asp.net 不用FileUpload控件上传图片,而是用获取到的图片路径将图片以二进制流存入数据库,那位大侠帮帮忙
asp.net
数据库
------解决方案--------------------写入的 。。
1.将图片转换为二进制数组(byte[])
byte[] fileData = this.FileUpload1.FileBytes;
2. 根据路径将文件转换为2进制数组
public byte[] returnbyte(string strpath)
{
// 以二进制方式读文件
FileStream fsMyfile = new FileStream(strpath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
// 创建一个二进制数据流读入器,和打开的文件关联
BinaryReader brMyfile = new BinaryReader(fsMyfile);
// 把文件指针重新定位到文件的开始
brMyfile.BaseStream.Seek(0, SeekOrigin.Begin);
byte[] bytes = brMyfile.ReadBytes(Convert.ToInt32(fsMyfile.Length.ToString()));
// 关闭以上new的各个对象
brMyfile.Close();
return bytes;
}
3img 类型得到二进制数组
public static byte[] Getbyte(Image img)
{
MemoryStream stream = new MemoryStream();
img.Save(stream, ImageFormat.Jpeg);
byte[] mydata = new byte[stream.Length];
mydata = stream.ToArray();
stream.Close();
return mydata;
}
读取image类型的数据并显示在网页上的方式如下:
1。直接返回image 类型
private System.Drawing.Image getImageDataFromOracle()
{
string sql = "select IMGDATA from t_img where imgID=100";
string strconn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringForOracle"].ToString();
OracleConnection oraConn = new OracleConnection(strconn);
OracleCommand oraComm = new OracleCommand(sql, oraConn);
oraConn.Open();
byte[] fileData = (byte[])oraComm.ExecuteScalar();
oraConn.Close();
System.IO.MemoryStream ms = new System.IO.MemoryStream(fileData);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
return img;
}
2.利用页面输入来显示图片
页面ImageShow.aspx (Page_Load方法)
protected void Page_Load(object sender, EventArgs e)
{
byte[] b_logoImg = (byte[])dt_channelImg.Rows[0]["LogoImage"]; //得到byte[] 数组,这里只是举个例子
if (b_logoImg.Length > 0)
{
System.Drawing.Image logoImg;
MemoryStream ms = new MemoryStream(b_logoImg);
Response.Clear();
Response.ContentType = "image/gif";
Response.OutputStream.Write(b_logoImg, 0, b_logoImg.Length);
Response.End();
}
}
图片路径写成为:<img src = "ImageShow.aspx" />
------解决方案-------------------- private void DownLoadImage(DataSet ds)
{
try
{
//File.Delete(Serve