【Vegas原创】从SQL中添加读取图片[参考孟子e章]
1,添加图片页面:fckeditor.aspx
<asp:Panel ID="pnlAddBanner" runat="server" CssClass="modalPopup" style="display:none;width:auto;">
<asp:Panel ID="pnlAddBannerDrg" runat="server" Style="width:auto;cursor: move;background-color:#DDDDDD;border:solid 1px Gray;color:Black;text-align:center;">
<div>
<p> 上传图片格式仅支援.jpg.gif.png等格式,单一档案大小为1MB。</p>
</div>
</asp:Panel>
<br />
Banner名<asp:TextBox ID="txtBannerName" runat="server"></asp:TextBox><br />
选择图片<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<p style="text-align:center;"> <asp:Button ID="btnBannerOK" runat="server" Text="上传" OnClick="btnBannerOK_Click" />
<asp:Button ID="btnBannerCancel" runat="server" Text="关闭" /></p><div><p>
</p>
</div>
</asp:Panel>
<asp:Panel ID="pnlAddBannerDrg" runat="server" Style="width:auto;cursor: move;background-color:#DDDDDD;border:solid 1px Gray;color:Black;text-align:center;">
<div>
<p> 上传图片格式仅支援.jpg.gif.png等格式,单一档案大小为1MB。</p>
</div>
</asp:Panel>
<br />
Banner名<asp:TextBox ID="txtBannerName" runat="server"></asp:TextBox><br />
选择图片<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<p style="text-align:center;"> <asp:Button ID="btnBannerOK" runat="server" Text="上传" OnClick="btnBannerOK_Click" />
<asp:Button ID="btnBannerCancel" runat="server" Text="关闭" /></p><div><p>
</p>
</div>
</asp:Panel>
后台:
protected void btnBannerOK_Click(object sender, EventArgs e)
{
//存储Banner图片
if (FileUpload1.HasFile)
{
string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
if (fileExt.ToLower() == ".gif" || fileExt.ToLower() == ".png" || fileExt.ToLower() == ".jpg")
{
try
{
//得到提交的文件
Stream fileDataStream = FileUpload1.PostedFile.InputStream;
//得到文件大小
int fileLength = FileUpload1.PostedFile.ContentLength;
//创建数组
byte[] fileData = new byte[fileLength];
//把文件流填充到数组
fileDataStream.Read(fileData, 0, fileLength);
//得到文件类型
string fileType = FileUpload1.PostedFile.ContentType;
//得到文件名字
string fileTitle = txtBannerName.Text.Trim();
//连接数据库,并插入数据
SqlParameter[] sqlParam = new SqlParameter[3];
sqlParam[0] = new SqlParameter("@myfilename", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "@myfilename", DataRowVersion.Default, fileTitle);
sqlParam[1] = new SqlParameter("myfile",SqlDbType.Image);
sqlParam[1].Value = fileData;
sqlParam[2] = new SqlParameter("@filetype", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "@filetype", DataRowVersion.Default,fileType);
db.ExecSPCommand("sp_platform_addImg", sqlParam);
//FileUpload1.SaveAs(Server.MapPath("upload") + "\\" + FileUpload1.FileName);
//lblMsg.Text = "客户端路径:" + FileUpload1.PostedFile.FileName + "<br>" +
// "文件名:" + System.IO.Path.GetFileName(FileUpload1.FileName) + "<br>" +
// "文件扩展名:" + System.IO.Path.GetExtension(FileUpload1.FileName) + "<br>" +
// "文件大小:" + FileUpload1.PostedFile.ContentLength + " KB<br>" +
// "文件MIME类型:" + FileUpload1.PostedFile.ContentType + "<br>" +
// "保存路径:" + Server.MapPath("upload") + "\\" + FileUpload1.FileName;
}
catch (Exception ex)
{
lblMsg.Text = "发生错误:" + ex.Message.ToString();
}
}
else
{
lblMsg.Text = "只允许上传gif、png、jpg文件!";
}
}
else
{
lblMsg.Text = "没有选择要上传的文件!";
}
}
{
//存储Banner图片
if (FileUpload1.HasFile)
{
string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
if (fileExt.ToLower() == ".gif" || fileExt.ToLower() == ".png" || fileExt.ToLower() == ".jpg")
{
try
{
//得到提交的文件
Stream fileDataStream = FileUpload1.PostedFile.InputStream;
//得到文件大小
int fileLength = FileUpload1.PostedFile.ContentLength;
//创建数组
byte[] fileData = new byte[fileLength];
//把文件流填充到数组
fileDataStream.Read(fileData, 0, fileLength);
//得到文件类型
string fileType = FileUpload1.PostedFile.ContentType;
//得到文件名字
string fileTitle = txtBannerName.Text.Trim();
//连接数据库,并插入数据
SqlParameter[] sqlParam = new SqlParameter[3];
sqlParam[0] = new SqlParameter("@myfilename", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "@myfilename", DataRowVersion.Default, fileTitle);
sqlParam[1] = new SqlParameter("myfile",SqlDbType.Image);
sqlParam[1].Value = fileData;
sqlParam[2] = new SqlParameter("@filetype", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "@filetype", DataRowVersion.Default,fileType);
db.ExecSPCommand("sp_platform_addImg", sqlParam);
//FileUpload1.SaveAs(Server.MapPath("upload") + "\\" + FileUpload1.FileName);
//lblMsg.Text = "客户端路径:" + FileUpload1.PostedFile.FileName + "<br>" +
// "文件名:" + System.IO.Path.GetFileName(FileUpload1.FileName) + "<br>" +
// "文件扩展名:" + System.IO.Path.GetExtension(FileUpload1.FileName) + "<br>" +
// "文件大小:" + FileUpload1.PostedFile.ContentLength + " KB<br>" +
// "文件MIME类型:" + FileUpload1.PostedFile.ContentType + "<br>" +
// "保存路径:" + Server.MapPath("upload") + "\\" + FileUpload1.FileName;
}
catch (Exception ex)
{
lblMsg.Text = "发生错误:" + ex.Message.ToString();
}
}
else
{
lblMsg.Text = "只允许上传gif、png、jpg文件!";
}
}
else
{
lblMsg.Text = "没有选择要上传的文件!";
}
}
注:1.db是公用类 DBAccess() ;
2.存储图片的时候必须是procdure,不能直接写insert语句,否则图片显示不出来,只显示:system.byte[].
2.读取图片页面:readimage.aspx
protected void Page_Load(object sender, EventArgs e)
{
DBAccess db = new DBAccess();
string strSql = "select * from platform_bannerimg where myfilename='" + Request.QueryString["myfilename"] + "'";
SqlDataReader dr = db.GetDataReader(strSql);
if (dr.Read())
{
Response.Clear();
Response.ContentType = dr["FileType"].ToString();
Response.BinaryWrite((byte[])dr["MyFile"]);
}
}
{
DBAccess db = new DBAccess();
string strSql = "select * from platform_bannerimg where myfilename='" + Request.QueryString["myfilename"] + "'";
SqlDataReader dr = db.GetDataReader(strSql);
if (dr.Read())
{
Response.Clear();
Response.ContentType = dr["FileType"].ToString();
Response.BinaryWrite((byte[])dr["MyFile"]);
}
}
前台不用写代码
3.显示图片页面(本示例 添加图片页面和显示图片页面在同一页面)fckeditor.aspx
<tr style="color: #000000"><td style="width: 130px">Banner</td><td >
<asp:Image ID="imgBanner" runat="server" /><br />
<asp:DropDownList ID="drpBanner" runat="server" AutoPostBack="True" OnSelectedIndexChanged="drpBanner_SelectedIndexChanged">
</asp:DropDownList>
<asp:LinkButton ID="lbtnAddBanner" runat="server">新增Banner</asp:LinkButton>
<br />
</td></tr>
<asp:Image ID="imgBanner" runat="server" /><br />
<asp:DropDownList ID="drpBanner" runat="server" AutoPostBack="True" OnSelectedIndexChanged="drpBanner_SelectedIndexChanged">
</asp:DropDownList>
<asp:LinkButton ID="lbtnAddBanner" runat="server">新增Banner</asp:LinkButton>
<br />
</td></tr>
后台:
page_load中添加:
//绑定Banner Image
imgBanner.ImageUrl = "readimage.aspx?myfilename=vegas";
imgBanner.ImageUrl = "readimage.aspx?myfilename=vegas";
protected void drpBanner_SelectedIndexChanged(object sender, EventArgs e)
{
imgBanner.ImageUrl = "readimage.aspx?myfilename="+drpBanner.SelectedValue.Trim();
}
{
imgBanner.ImageUrl = "readimage.aspx?myfilename="+drpBanner.SelectedValue.Trim();
}
4.数据库:
USE [VS2005]
GO
/****** 对象: Table [dbo].[platForm_BannerImg] 脚本日期: 03/07/2008 09:48:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[platForm_BannerImg](
[MyFileName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[MyFile] [image] NULL,
[FileType] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
/****** 对象: Table [dbo].[platForm_BannerImg] 脚本日期: 03/07/2008 09:48:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[platForm_BannerImg](
[MyFileName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[MyFile] [image] NULL,
[FileType] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
5.存储过程:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER proc [dbo].[sp_platform_addImg]
@myfilename nvarchar(50),
@myfile image,
@filetype nvarchar(50)
as
begin
insert into platform_bannerimg(myfilename,myfile,filetype)
values
(@myfilename,@myfile,@filetype)
end
set QUOTED_IDENTIFIER ON
go
ALTER proc [dbo].[sp_platform_addImg]
@myfilename nvarchar(50),
@myfile image,
@filetype nvarchar(50)
as
begin
insert into platform_bannerimg(myfilename,myfile,filetype)
values
(@myfilename,@myfile,@filetype)
end
喜欢请赞赏一下啦^_^