如题
解决方案 »
- 如何实现此功能?
- 分给高点,想在几个项目中共享一个全局变量,这个全局变量不用session.哪位给一段代码学习一下。???
- 现在要在.net中要将数据读出到excels 并且要按自己的要求显示在excels 中,可以通过应用程序读出也可以通过asp页面读出。格式如下:
- 如何从别的页面传来的参数查询数据库并且绑定到新的GridView上?
- .net 问题(急要)
- 如何在绑定好的datagrid中添加列
- VS2005大量教程+组件+源码+AJAX范例下载,需要的朋友,赶快进来抢
- ListBox的问题
- js在windows2003系统为什么不执行!在2000下都可以的!谢谢
- 怎么在动态建立动态创建DataGrid的模版列的时候在模板列里加入html
- 如何下载后更新GridView
- 急求javascript上传图片源码!!!
保存进数据库,就是把图片读到流再以byte[]形式存入,这个不会,加100分我也懒得答.
从数据库取出,用DataAdapter.fill到一个DataSet 再将DataSet中的存放该图片的单元格的数据转换为byte[],
response.clear();
response.contentType="image/jpg";// 可以不写
response.write(byte[]);
response.end();
注意这里你可以单独建立一个页面,接收参数QuerryString来返回对应的图片.//例程
page_onload(....)
{
string q=request.querrystring["q"];
dataset ds=........
byte[] bts=(byte[])ds.Tables[0].rows[0][n];//转换不知道是不是这样写,手上没工具不记得了.
response.clear();
response.contentType="image/jpg";// 可以不写
response.write(byte[]);
response.end();
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls; using System.IO;
using System.Data.SqlClient;public partial class tupiancaozuo : System.Web.UI.Page
{
private string fileName = "";
private static SqlConnection conn = null; protected void Page_Load(object sender, EventArgs e)
{
ConnectDB(); }
protected void Button1_Click(object sender, EventArgs e) { WriteImage(); } //得到文件名
private string GetFile()
{
HttpPostedFile file = File1.PostedFile;
fileName = file.FileName; return fileName;
}
//读取文件内容
private byte[] ReadFile()
{
FileStream file = File.OpenRead(GetFile());
byte[] content = new byte[file.Length];
file.Read(content, 0, content.Length);
file.Close(); return content;
} //连接数据库
private void ConnectDB()
{
string connStr = "server=(local)\\SQLEXPRESS;uid=sa;pwd=123;database=zonghe;";
conn = new SqlConnection(connStr);
conn.Open();
}
//写入图片到数据库中
private void WriteImage()
{
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "insert into images(image,stype) values(@image,@type)";
comm.CommandType = CommandType.Text;
SqlParameter param = comm.Parameters.Add("@image", SqlDbType.Image);
param.Value = ReadFile();
param = comm.Parameters.Add("@type", SqlDbType.NVarChar);
param.Value = GetContentType(new FileInfo(fileName).Extension.Remove(0, 1)); if (comm.ExecuteNonQuery() == 1)
Response.Write("Successful");
else
Response.Write("Fail"); conn.Close();
} //获取图片的后缀名
private string GetContentType(string extension)
{
string type = ""; if (extension.Equals("jpg") || extension.Equals("JPG"))
type = "jpeg";
else
type = extension; return "image/" + type;
} //从数据库中读取图片
private void ReadImage()
{
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "select image,stype from images";
comm.CommandType = CommandType.Text; SqlDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
Response.ContentType = reader["stype"].ToString();//读写类型 一定要设置否则浏览器会当作文本输出
Response.BinaryWrite((byte[])reader["image"]);//图片数据
} Response.Write("Successful");
Response.End(); conn.Close();
} protected void Button2_Click(object sender, EventArgs e)
{
try
{
ReadImage();
}
catch(Exception ep)
{
conn.Close();
Response.End();
}
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls; using System.IO;
using System.Data.SqlClient;public partial class tupiancaozuo : System.Web.UI.Page
{
private string fileName = "";
private static SqlConnection conn = null; protected void Page_Load(object sender, EventArgs e)
{
ConnectDB(); }
protected void Button1_Click(object sender, EventArgs e) { WriteImage(); } //得到文件名
private string GetFile()
{
HttpPostedFile file = File1.PostedFile;
fileName = file.FileName; return fileName;
}
//读取文件内容
private byte[] ReadFile()
{
FileStream file = File.OpenRead(GetFile());
byte[] content = new byte[file.Length];
file.Read(content, 0, content.Length);
file.Close(); return content;
} //连接数据库
private void ConnectDB()
{
string connStr = "server=(local)\\SQLEXPRESS;uid=sa;pwd=123;database=zonghe;";
conn = new SqlConnection(connStr);
conn.Open();
}
//写入图片到数据库中
private void WriteImage()
{
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "insert into images(image,stype) values(@image,@type)";
comm.CommandType = CommandType.Text;
SqlParameter param = comm.Parameters.Add("@image", SqlDbType.Image);
param.Value = ReadFile();
param = comm.Parameters.Add("@type", SqlDbType.NVarChar);
param.Value = GetContentType(new FileInfo(fileName).Extension.Remove(0, 1)); if (comm.ExecuteNonQuery() == 1)
Response.Write("Successful");
else
Response.Write("Fail"); conn.Close();
} //获取图片的后缀名
private string GetContentType(string extension)
{
string type = ""; if (extension.Equals("jpg") || extension.Equals("JPG"))
type = "jpeg";
else
type = extension; return "image/" + type;
} //从数据库中读取图片
private void ReadImage()
{
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "select image,stype from images";
comm.CommandType = CommandType.Text; SqlDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
Response.ContentType = reader["stype"].ToString();//读写类型 一定要设置否则浏览器会当作文本输出
Response.BinaryWrite((byte[])reader["image"]);//图片数据
} Response.Write("Successful");
Response.End(); conn.Close();
} protected void Button2_Click(object sender, EventArgs e)
{
try
{
ReadImage();
}
catch(Exception ep)
{
conn.Close();
Response.End();
}
}
}