一、ASP.NET上传文件数据库。
存储文件的数据库中的字段为jimage,类型为image。
在代码中定义类型为byte[]的一个变量buf,在上传组件的PostFile中,从它的InputStream读出字节数组,将buf赋给数据字段jimage就可以了。
int len = this.File1.PostedFile.ContentLength;
byte[] buf = new byte[len];
Stream i = this.File1.PostedFile.InputStream;
i.Read(buf,0,buf.Length);
news.jimage=buf;
//news为新闻类,jimage为它的图片属性,即对应表中的image
i.Close();
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
显示图像:
图片的显示也很简单,在Persister中注意一下:
SqlDataReader reader=SqlHelper.ExecuteReader
("select jimage from news");
if( reader.Read() )
{
news.jimage=(byte[])reader["jimage"];
}
reader.Close();
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
得到byte[]的内容,要显示也比较简单,在Page_Load()方法中加两句话即可:
Response.ContentType="image/jpeg";
Response.BinaryWrite(ti.content);
- 1.
- 2.
这样就可以输出图像了,如果想对图像做一点调整,如旋转,转换格式、获得图片格式(是jpg 还是 gif),请参考下面代码:
//同样,声明输出不是HTML而是image
Response.ContentType="image/jpeg";
//从byte[]得到一个image对象
System.Drawing.Image bmap = Bitmap.FromStream
(new MemoryStream(ti.content));
//操作一下这个图像
bmap.RotateFlip(RotateFlipType.Rotate180FlipY);
//输出到页面上
bmap.Save(Response.OutputStream,System.
Drawing.Imaging.ImageFormat.Jpeg);
//释放image
bmap.Dispose();
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
要显示图片在某一个image控件上,可采用下法:
要显示图片的位置放一个image控件然后将它的src指向这个页面就行了!
例如:
页面:ViewImage.aspx
〈%@Import Namespace="System.IO"% 〉
〈%@Import Namespace="System.Data"% 〉
〈%@Import Namespace="System.Data.SqlClient"% 〉
〈%@ Page Language="C#" Debug="True" % 〉
〈script runat="server" 〉
private void Page_Load(Object sender, System.EventArgs e)
{
string imgid =Request.QueryString["UserID"];
string connstr="data source=(local);initial
catalog=Test;integrated security=SSPI;persist
security info=True;packet size=4096";
string sql="SELECT IMGTITLE,imgdata,
imgtype FROM ImageStore WHERE id = '"+ imgid "'";
SqlConnection connection = new SqlConnection(connstr);
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader dr = command.ExecuteReader();
if(dr.Read())
{
Response.ContentType = dr["imgtype"].ToString();
Response.BinaryWrite( (byte[]) dr["imgdata"] );
Response.Write(dr["IMGTITLE"].ToString());
}
connection.Close();
}
〈/script 〉
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
显示图片的页面上放一个image控件imgZYF 在后代码中写:imgZYF.ImageUrl =“ViewImage.aspx?UserID=" +userId
二、ASP.NET上传文件到服务器的磁盘:
页面文件:upload01.aspx
〈%@Pagelanguage="c#"Codebehind="upload01.aspx.cs"
AutoEventWireup="false"Inherits="upload01.upload01"%〉
〈!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN"〉
〈HTML〉
〈HEAD〉
〈title〉上传到磁盘〈/title〉
〈/HEAD〉
〈body〉
〈formid="Form1"method="post"runat="server"〉
〈TABLEheight="300"cellSpacing="1"cellPadding="1"
width="500"border="0"class="bigtable-bj"
align="center"〉
〈TR〉
〈TD〉〈FONTface="宋体"〉
〈TABLEid="Table1"style="WIDTH:384px;HEIGHT:54px"
cellSpacing="1"cellPadding="1"width="384"
border="0"align="center"〉
〈TR〉
〈TD〉选择文件:〈/TD〉
〈TD〉〈INPUTtype="file"id="myfile"runat="server"〉〈/TD〉
〈/TR〉
〈TR〉
〈TDstyle="HEIGHT:21px"〉输入备注:〈/TD〉
〈TDstyle="HEIGHT:21px"〉
〈asp:TextBoxid="TextBox1"runat="server"〉〈/asp:TextBox〉〈/TD〉
〈/TR〉
〈TR〉
〈TD〉〈/TD〉
〈TD〉〈INPUTtype="button"value="上传文件"
runat="server"id="Button1"name="Button1"〉
〈INPUTtype="submit"value="清空选择"〉〈/TD〉
〈/TR〉
〈/TABLE〉
〈/FONT〉
〈/TD〉
〈/TR〉
〈/TABLE〉
〈/form〉
〈/body〉
〈/HTML〉
后置代码:upload01.aspx
usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Web;
usingSystem.Web.SessionState;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;
namespaceupload01
{
publicclassupload01:System.Web.UI.Page
{
protectedSystem.Web.UI.HtmlControls.HtmlInputButtonButton1;
protectedSystem.Web.UI.WebControls.TextBoxTextBox1;
protectedSystem.Web.UI.HtmlControls.HtmlInputFilemyfile;
privatevoidPage_Load(objectsender,System.EventArgse)
{
//昨夜风www.zuoyefeng.com
}
privatevoidButton1_ServerClick
(objectsender,System.EventArgse)
{
//取得客户端路径及文件名
stringstr=myfile.PostedFile.FileName;
//取得ASP.NET上传文件类型,如.jpg
stringfilename2=str.Substring
(str.LastIndexOf(".")).ToString().Trim();
//取得ASP.NET上传文件大小,单位K
doublefilesize=myfile.PostedFile.ContentLength/1024.00;
//以时间刻度定义文件名
stringfilename1=DateTime.Now.Ticks.ToString();
myfile.PostedFile.SaveAs(Server.MapPath
("/upload01/"+filename1+filename2));
//将文件名及相关信息存到数据库中
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
将ASP.NET上传文件到磁盘中,在表中将文件地址或路径记录下来,这样就可以在后面的程序来引用了。
【编辑推荐】