新手求救!如何将图片写入数据库中并读出来显示?在线等。 新手求救!如何将图片写入数据库中并读出来显示?在线等。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://dotnet.aspx.cc/ShowDetail.aspx?id=2A5DD7C6-A45A-48AB-A2E8-342A29F17506 写入数据库//上传if(myFile.ContentLength != 0){ try { System.Web.HttpPostedFile myFile = this.Request.Files[0];// string tmpFileName = myFile.FileName;// string myFileName = tmpFileName.Substring(tmpFileName.LastIndexOf("."));// string myFileMimeType = myFile.ContentType();// myFile.SaveAs(this.Server.MapPath("../" + myFileName)); //读取到数组里面 System.IO.Stream mystream = myFile.InputStream; byte[] Buffer = new byte[myFile.ContentLength]; mystream.Read(Buffer,0,myFile.ContentLength); //打开数据库 OracleConnection cn = new OracleConnection(ConfigurationSettings.AppSettings["sysDSN"]); cn.Open(); //用参数方式写入数据库 OracleCommand myComm = cn.CreateCommand(); string sql = "insert into tmp(tmp_id,tmp_blob) values(tmp_seq.nextval,:tmp_blob)"; myComm.CommandText = sql; myComm.Parameters.Add(":tmp_blob",OracleType.Blob,Buffer.Length).Value = Buffer; myComm.ExecuteNonQuery(); } catch { //此处可加错误显示 } finally { cn.Close(); }}在页面中,放一个Image控件,在后台指定它的链接地址如下:----------------------------------------------------------------------//为方便,写一个固定ID号tmp_id=103this.Image1.ImageUrl = "showimg.aspx?tmp_id=103" ;下面是显示图片的页面showimg.aspx后台代码,该页面不需要放任何东西.--------------------------------------------------------------------//创建数据库连接OracleConnection myConnection = new OracleConnection(ConfigurationSettings.AppSettings["sysDSN"]);myConnection.Open();//打开数据库OracleCommand myCommand = myConnection.CreateCommand();string sql = "select tmp_blob from tmp where tmp_id = 103";myCommand.CommandText = sql;OracleDataReader myRead = myCommand.ExecuteReader();//开始读取myRead.Read();//这个方法更直接Byte[] Buffer = (Byte[])myRead[0];//OracleLob myLob = myRead.GetOracleLob(0);//长度是long,转为int32//int myLength = Convert.ToInt32(myLob.Length); //Byte[] Buffer = new byte[myLength]; //myLob.Read(Buffer,0,myLength);//输出this.Response.Clear();//输出mime类型,根据上传的文件取到的mimetype,是什么我忘记了,自己写上,不过不写也没有关系.this.Response.ContentType = "";this.Response.BinaryWrite(Buffer);this.Response.End(); 读出来的时候用Response.BinaryWrite方法能很好的控制图片属性吗?比如说大小、位置什么的。 读出来的时候用Response.BinaryWrite方法读取 然后 把这个图片作为 img 的元素 当然控制img的大小 和位置 就改变了 显示的图片的大小 和位置 帮找下错 静态类能实现接口吗 .NET服务器控件将引领时代潮流(个人观点) log4net xml日志乱码 望高手指点,谢谢了. 请教一下,都进来坐着喝杯咖啡 求一个权限审查的方案!! asp.net中怎么在邮件发送失败后,来得到服务器的返回邮件?高手请进 按钮事件不能执行!? 急呢!!没法做下去了 怎么动态添加变量? 急急急 我就20分了 都给你们 asp中连数据库删除这个是哪出错了啊? 获取网站名称急!急!急! 有谁可以介绍一下做gd柱形图的问题
if(myFile.ContentLength != 0)
{
try
{
System.Web.HttpPostedFile myFile = this.Request.Files[0];
// string tmpFileName = myFile.FileName;
// string myFileName = tmpFileName.Substring(tmpFileName.LastIndexOf("."));
// string myFileMimeType = myFile.ContentType();
// myFile.SaveAs(this.Server.MapPath("../" + myFileName)); //读取到数组里面
System.IO.Stream mystream = myFile.InputStream;
byte[] Buffer = new byte[myFile.ContentLength];
mystream.Read(Buffer,0,myFile.ContentLength); //打开数据库
OracleConnection cn = new OracleConnection(ConfigurationSettings.AppSettings["sysDSN"]);
cn.Open(); //用参数方式写入数据库
OracleCommand myComm = cn.CreateCommand();
string sql = "insert into tmp(tmp_id,tmp_blob) values(tmp_seq.nextval,:tmp_blob)";
myComm.CommandText = sql;
myComm.Parameters.Add(":tmp_blob",OracleType.Blob,Buffer.Length).Value = Buffer;
myComm.ExecuteNonQuery();
}
catch
{
//此处可加错误显示
}
finally
{
cn.Close();
}
}
在页面中,放一个Image控件,在后台指定它的链接地址如下:
----------------------------------------------------------------------
//为方便,写一个固定ID号tmp_id=103
this.Image1.ImageUrl = "showimg.aspx?tmp_id=103" ;
下面是显示图片的页面showimg.aspx后台代码,该页面不需要放任何东西.
--------------------------------------------------------------------
//创建数据库连接
OracleConnection myConnection = new OracleConnection(ConfigurationSettings.AppSettings["sysDSN"]);
myConnection.Open();//打开数据库
OracleCommand myCommand = myConnection.CreateCommand();
string sql = "select tmp_blob from tmp where tmp_id = 103";
myCommand.CommandText = sql;
OracleDataReader myRead = myCommand.ExecuteReader();//开始读取
myRead.Read();
//这个方法更直接
Byte[] Buffer = (Byte[])myRead[0];
//OracleLob myLob = myRead.GetOracleLob(0);
//长度是long,转为int32
//int myLength = Convert.ToInt32(myLob.Length);
//Byte[] Buffer = new byte[myLength];
//myLob.Read(Buffer,0,myLength);//输出
this.Response.Clear();
//输出mime类型,根据上传的文件取到的mimetype,是什么我忘记了,自己写上,不过不写也没有关系.
this.Response.ContentType = "";
this.Response.BinaryWrite(Buffer);
this.Response.End();
然后 把这个图片作为 img 的元素
当然控制img的大小 和位置
就改变了 显示的图片的大小 和位置