惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
NISL@THU
NISL@THU
E
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
K
Kaspersky official blog
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
S
Schneier on Security
G
GRAHAM CLULEY
The Hacker News
The Hacker News
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
爱范儿
爱范儿
P
Privacy International News Feed
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
S
Securelist
G
Google Developers Blog
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
美团技术团队
F
Fortinet All Blogs
小众软件
小众软件
Recorded Future
Recorded Future
V
Visual Studio Blog
B
Blog RSS Feed
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
Latest news
Latest news
Spread Privacy
Spread Privacy
H
Heimdal Security Blog

博客园 - 钛网络

真正部署中的apache+tomcat负载均衡 MSSQL处理死锁存储过程 GridView使用大全 打印机每天都要重新连接 数据库安装时挂起问题 在运行 32 位版本的 SQL Server 2000 SP4 的计算机上启用 AWE 时有些内存不可用 SQL Server 2000安装故障 关于asp.net 2.0的认知太少的问题 SQL 操作————简单查询初探 SQL 操作————不等联接 SQL SERVER中直接循环写入数据 SQL Server日期计算 SQL Server安全规划全攻略 SQL Server:创建索引视图 SQL Server 2000的安全配置 Oracle大数据量分页通用存储过程 ASP.Net生成静态HTML页 2 ASP.NET上传文件的实例 ASP.NET封装的数据库访问基类
ASP.NET文件上传下载
钛网络 · 2005-12-21 · via 博客园 - 钛网络

文件上传

一.     在Form中一定要将encType设为"multipart/form-data":
<form id="WebForm3" method="post" encType="multipart/form-data" runat="server" >

二.     判断是否有文件上传了:
当用户没有选择任何要上传的文件,即HtmlInputFile控件中的文本框为空时点击了上传按钮后,在服务端得到的File1.PostedFile对象不是null,而是有对象的,所以不能用(File1.PostedFile == null)来判断是否上传了文件,用(File1.PostedFile.ContentLength != 0)来判断比较好

三.     判断上传文件MIMIE类型:
文件上传后可以用File1.PostedFile.ContentType来读取这个文件的MIMIE类型,这个MIMIE类型是系统通过上传文件的后缀名来获得的。

四.     保存上传的文件:

1.       文件可以通过File1.PostedFile.SaveAs(path) //path是服务器上的物理路径,来保存文件。


if(File1.PostedFile.ContentLength != 0)

{

       StringBuilder myStr = new StringBuilder();

       myStr.Append("文件名称:" + File1.PostedFile.FileName);

       myStr.Append("<br>");

       myStr.Append("文件类型:" + File1.PostedFile.ContentType);

       myStr.Append("<br>");

       myStr.Append("文件长度:" + File1.PostedFile.ContentLength.ToString());

       myStr.Append("<br>");

       string path = Server.MapPath("./");  //当前路径

       string fileName = File1.PostedFile.FileName.Substring(File1.PostedFile.FileName.LastIndexOf('\\')+1);

       path += fileName;

       if(File.Exists(path) == true)

       {

              Label1.Text = "服务器上已经有了你正在上传的文件:" + fileName;

              return;

       }

       File1.PostedFile.SaveAs(path);

       myStr.Append("保存完毕!");

       myStr.Append("<br>");

       Label1.Text = myStr.ToString();

}

else

{

       Label1.Text = "你没有选择要上载的文件或者上传的文件长度为0!";

}


2.       文件也可以通过二进制的读取后存放到数据库的二进制的字段中:
byte[] fileCont = new byte[File1.PostedFile.ContentLength];
File1.PostedFile.InputStream.Read(fileCont,0, File1.PostedFile.ContentLength);
然后将此字节数组fileCont赋给数据库的二进制字段的参数,写到数据库中。

文件下载

一.     服务端通过Response输出相应的HTTP Response Headers信息,和要下载的文件的数据来把文件发送到客户端,HTTP Response Headers表现在html文件中是下面的形式:
<meta http-equiv="Content-Type" content="text/htm ">
http-equiv表示是Headers的名称,content表示这个Headers的值

二.     首先,要输出文件的MIME类型:
Page.Response.AddHeader( "Content-Type", “MIME类型” ); 

三.     其次,要输出下载的文件的打开位置和文件名:
Page.Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName );
content-disposition 的 HTTP response header 允许指定文档表示的信息。使用这种 header ,你就可以将文档指定成单独打开(而不是在浏览器中打开),还可以根据用户的操作来显示。如果用户要保存文档,你还可以为该文档建议一个文件名。这个建议名称会出现在 Save As 对话框的“文件名”栏中。
打开位置:
attachment ―― 表示作为附件发送到客户端,客户端将单独打开此文件。
inline ―― 表示将在浏览器中打开这个文件。
文件名:
filename ―― 表示发送到客户端文件的文件名。

四.     准备发送到客户端的文件数据:

1.       先将不同类型来源的数据转成byte类型的数组,再通过Response.BinaryWrite方法发送到客户端:

1.1.      读取文件来获得byte数组:

string FileName;  //生成或获取要发送到客户端的文件名

string filePath = Server.MapPath("./") + FileName;  //假设文件在当前目录下

if(File.Exists(filePath) == false)

{

       //服务器上没有这个文件

       return;

}

FileStream myFile = File.OpenRead(filePath);  //读取文件进入FileStream

byte[] fileCont = new byte[myFile.Length];

myFile.Read(fileCont,0,(int)myFile.Length);   //将文件流中的内容转成byte数组


1.2.      在数据库的二进制字段中读取:

//从url获取图片的id

string ImageId = Request.QueryString["img"];

//构建查询语句

string sqlText = "SELECT img_data, img_contenttype FROM Image WHERE img_pk = " + ImageId;

SqlConnection connection = new SqlConnection( ConfigurationSettings.AppSettings["DSN"].ToString() );

SqlCommand command = new SqlCommand( sqlText, connection);

connection.Open();

SqlDataReader dr = command.ExecuteReader();

if ( dr.Read())

{

       byte[] fileCont = (byte[]) dr["img_data"] ;

}

connection.Close();


1.3.      从internet上读取文件:

HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create( "http://www.test.com/test.xls ");

HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();

Stream readStream = myWebResponse.GetResponseStream();

byte[] bytes = new byte[readStream.Length];

bytes = readStream.Read(bytes,0,readStream.Length);


通过上述三种方法获得的文件内容的byte数组就可以用来输出了:
Page.Response.BinaryWrite(fileCont);

Page.Response.End();

2.       直接读取文件输出:

string FileName;  //生成或获取要发送到客户端的文件名

string filePath = Server.MapPath("./") + FileName;  //假设文件在当前目录下

if(File.Exists(filePath) == false)

{

       //服务器上没有这个文件

       return;

}

Page.Response.Clear();

Page.Response.AddHeader( "Content-Type", "image/gif" );  //根据MIME的不同设置

Page.Response.AddHeader("Content-Disposition", "inline;filename=" + filePath);

Page.Response.WriteFile(filePath);

Page.Response.End();