All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
Asp.Net生成静态页面实例代码_[Asp.Net教程]
ASP.NET,2.0移动开发之设备筛选器的应用_.net资料_编程技术
ASP.NET页面中冻结DataGrid的列或头部_.net资料_编程技术
ASP.NET中动态控制RDLC报表_[Asp.Net教程]_0
关于C#格式化日期时间_[Asp.Net教程]
asp.net2.0中Server对象的方法和属性
配置ASP.net的运行环境_[Asp.Net教程]
AJAX在Post中文的时候解决乱码的方法_[AJAX教程]
在delphi中面向对象的特性
ASP.NET中Cookie的使用介绍_[Asp.Net教程]
Reading number is top 10 pictures
南昌铁路局攸县车站125户铁路职工微利房终成骗局
Chinese paper-cut grilles art appreciation1
中国处女图鉴2
西游四格漫画(三)
全球清廉国家排行
Born After 90 Beijing sports university campus flower photos5
七步解决性冷淡
In the world the most mysterious 21 place landscape4
China telecom 114 spokesman MeiYanXu2
Take you to walk into the most true north Korea rural4
Download software ranking
Unix video tutorial14
Call Of Duty5
VC++6.0简体中文版
Visual C++界面编程技术
VeryCD电驴(EasyMule) V1.1.9 Build09081
c#程序设计案例教程
Love the forty days
Professional killers2 for Android
美女写真3
SP4 for SQL2000
delv published in(发表于) 2014/1/24 9:04:20 Edit(编辑)
ASP.NET上传文件面面观_[Asp.Net教程]

ASP.NET上传文件面面观_[Asp.Net教程]

ASP.NET上传文件面面观_[Asp.Net教程]




























一、上传到数据库。

(sqlserver为例)
存储文件的数据库中的字段为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" %>







显示图片的页面上放一个image控件imgZYF 在后代码中写:
imgZYF.ImageUrl =“ViewImage.aspx?UserID=" +userId




二、上传到服务器的磁盘:

页面文件:upload01.aspx

















<%@ Page language="c#" Codebehind="upload01.aspx.cs" AutoEventWireup="false" Inherits="upload01.upload01" %>



上传到磁盘









align="center">








border="0" align="center">





选择文件:















输入备注:





















&nbsp;




























后置代码:upload01.aspx






using System;
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;


namespace upload01
{




public class upload01 : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputButton Button1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.HtmlControls.HtmlInputFile myfile;

private void Page_Load(object sender, System.EventArgs e)
{

}







private void Button1_ServerClick(object sender, System.EventArgs e)
{
//取得客户端路径及文件名
string str=myfile.PostedFile.FileName;
//取得文件类型,如.jpg
string filename2=str.Substring(str.LastIndexOf(".")).ToString().Trim();
// 取得文件大小,单位K
double filesize=myfile.PostedFile.ContentLength/1024.00;
//以时间刻度定义文件名
string filename1=DateTime.Now.Ticks.ToString();
myfile.PostedFile.SaveAs(Server.MapPath("/upload01/"+filename1+filename2));
//将文件名及相关信息存到数据库中




}
}
}






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

来源:CSDN















































添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.