网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

ASP.NET上传文件的示例分析

ASP.NET上传文件的示例分析,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

成都创新互联公司公司2013年成立,是专业互联网技术服务公司,拥有项目做网站、网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元巧家做网站,已为上家服务,为巧家各地企业和个人服务,联系电话:18980820575

一、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();

显示图像:

图片的显示也很简单,在Persister中注意一下:

SqlDataReader reader=SqlHelper.ExecuteReader  ("select jimage from news");     if( reader.Read() )  {   news.jimage=(byte[])reader["jimage"];  }  reader.Close();

得到byte[]的内容,要显示也比较简单,在Page_Load()方法中加两句话即可:

Response.ContentType="image/jpeg";  Response.BinaryWrite(ti.content);

这样就可以输出图像了,如果想对图像做一点调整,如旋转,转换格式、获得图片格式(是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();

要显示图片在某一个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  〉

显示图片的页面上放一个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));  //将文件名及相关信息存到数据库中  }  }  }

将ASP.NET上传文件到磁盘中,在表中将文件地址或路径记录下来,这样就可以在后面的程序来引用了。

关于ASP.NET上传文件的示例分析问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


新闻名称:ASP.NET上传文件的示例分析
链接地址:http://bjjierui.cn/article/gssccg.html

其他资讯