文件上传及下载 
 
 
文件上传一.     在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);  //读取文件进入FileStreambyte[] fileCont = new byte[myFile.Length];myFile.Read(fileCont,0,(int)myFile.Length);   //将文件流中的内容转成byte数组
 1.2.      在数据库的二进制字段中读取: //从url获取图片的idstring 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.via.com/aa.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();