文件上传及下载

** 文件上传 ** ** **

一. 在 Form 中一定要将 encType 设为 "multipart/form-data" :

  1<form enctype="multipart/form-data" id="WebForm3" method="post" runat="server">
  2
  3二.  判断是否有文件上传了:    
  4当用户没有选择任何要上传的文件,即  HtmlInputFile  控件中的文本框为空时点击了上传按钮后,在服务端得到的  File1.PostedFile  对象不是  null  ,而是有对象的,所以不能用(  File1.PostedFile == null  )来判断是否上传了文件,用(  File1.PostedFile.ContentLength != 0  )来判断比较好 
  5
  6三.  判断上传文件  MIMIE  类型:    
  7文件上传后可以用  File1.PostedFile.ContentType  来读取这个文件的  MIMIE  类型,这个  MIMIE  类型是系统通过上传文件的后缀名来获得的。 
  8
  9四.  保存上传的文件: 
 10
 111\.  文件可以通过  File1.PostedFile.SaveAs(path) //path  是服务器上的物理路径,来保存文件。 
 12
 13if(File1.PostedFile.ContentLength != 0) 
 14
 15{ 
 16
 17StringBuilder myStr = new StringBuilder(); 
 18
 19myStr.Append("  文件名称:  " + File1.PostedFile.FileName); 
 20
 21myStr.Append("<br/>"); 
 22
 23myStr.Append("  文件类型:  " + File1.PostedFile.ContentType); 
 24
 25myStr.Append("<br/>"); 
 26
 27myStr.Append("  文件长度:  " + File1.PostedFile.ContentLength.ToString()); 
 28
 29myStr.Append("<br/>"); 
 30
 31string path = Server.MapPath("./");  //  当前路径 
 32
 33string fileName = File1.PostedFile.FileName.Substring(File1.PostedFile.FileName.LastIndexOf('\\\')+1); 
 34
 35path += fileName; 
 36
 37if(File.Exists(path) == true) 
 38
 39{ 
 40
 41Label1.Text = "  服务器上已经有了你正在上传的文件  :" + fileName; 
 42
 43return; 
 44
 45} 
 46
 47File1.PostedFile.SaveAs(path); 
 48
 49myStr.Append("  保存完毕!  "); 
 50
 51myStr.Append("<br/>"); 
 52
 53Label1.Text = myStr.ToString(); 
 54
 55} 
 56
 57else 
 58
 59{ 
 60
 61Label1.Text = "  你没有选择要上载的文件或者上传的文件长度为  0  !  "; 
 62
 63}   
 64  
 65---  
 66  
 672\.  文件也可以通过二进制的读取后存放到数据库的二进制的字段中:    
 68byte[] fileCont = new byte[File1.PostedFile.ContentLength];   
 69File1.PostedFile.InputStream.Read(fileCont,0, File1.PostedFile.ContentLength);    
 70然后将此字节数组  fileCont  赋给数据库的二进制字段的参数,写到数据库中。 
 71
 72** 文件下载  ** ** **
 73
 74一.  服务端通过  Response  输出相应的  HTTP Response Headers  信息,和要下载的文件的数据来把文件发送到客户端,  HTTP Response Headers  表现在  html  文件中是下面的形式:    
 75<meta content="text/htm " http-equiv="Content-Type"/>   
 76http-equiv  表示是  Headers  的名称,  content  表示这个  Headers  的值 
 77
 78二.  首先,要输出文件的  MIME  类型:    
 79Page.Response.AddHeader( "Content-Type", “MIME  类型  ” ); 
 80
 81三.  其次,要输出下载的文件的打开位置和文件名:    
 82Page.Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName );   
 83content-disposition  的  HTTP response header  允许指定文档表示的信息。使用这种  header  ,你就可以将文档指定成单独打开(而不是在浏览器中打开),还可以根据用户的操作来显示。如果用户要保存文档,你还可以为该文档建议一个文件名。这个建议名称会出现在  Save As  对话框的“文件名”栏中。    
 84打开位置:    
 85attachment  ――  表示作为附件发送到客户端,客户端将单独打开此文件。    
 86inline  ――  表示将在浏览器中打开这个文件。    
 87文件名:    
 88filename  ――  表示发送到客户端文件的文件名。 
 89
 90四.  准备发送到客户端的文件数据: 
 91
 921\.  先将不同类型来源的数据转成  byte  类型的数组  ,  再通过  Response.BinaryWrite  方法发送到客户端  : 
 93
 941.1.  读取文件来获得  byte  数组: 
 95
 96string FileName;  //  生成或获取要发送到客户端的文件名 
 97
 98string filePath = Server.MapPath("./") + FileName;  //  假设文件在当前目录下 
 99
100if(File.Exists(filePath) == false) 
101
102{ 
103
104//  服务器上没有这个文件 
105
106return; 
107
108} 
109
110FileStream myFile = File.OpenRead(filePath);  //  读取文件进入  FileStream 
111
112byte[] fileCont = new byte[myFile.Length]; 
113
114myFile.Read(fileCont,0,(int)myFile.Length);  //  将文件流中的内容转成  byte  数组   
115  
116---  
117  
1181.2.  在数据库的二进制字段中读取: 
119
120//  从  url  获取图片的  id 
121
122string ImageId = Request.QueryString["img"]; 
123
124//  构建查询语句 
125
126string sqlText = "SELECT img_data, img_contenttype FROM Image WHERE img_pk = " + ImageId; 
127
128SqlConnection connection = new SqlConnection( ConfigurationSettings.AppSettings["DSN"].ToString() ); 
129
130SqlCommand command = new SqlCommand( sqlText, connection); 
131
132connection.Open(); 
133
134SqlDataReader dr = command.ExecuteReader(); 
135
136if ( dr.Read()) 
137
138{ 
139
140byte[] fileCont = (byte[]) dr["img_data"] ; 
141
142} 
143
144connection.Close();   
145  
146---  
147  
1481.3.  从  internet  上读取文件: 
149
150HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create( "http://www.via.com/aa.xls "); 
151
152HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse(); 
153
154Stream readStream = myWebResponse.GetResponseStream(); 
155
156byte[] bytes = new byte[readStream.Length]; 
157
158bytes = readStream.Read(bytes,0,readStream.Length);   
159  
160---  
161  
162通过上述三种方法获得的文件内容的  byte  数组就可以用来输出了:    
163Page.Response.BinaryWrite(fileCont); 
164
165Page.Response.End(); 
166
1672\.  直接读取文件输出  : 
168
169string FileName;  //  生成或获取要发送到客户端的文件名 
170
171string filePath = Server.MapPath("./") + FileName;  //  假设文件在当前目录下 
172
173if(File.Exists(filePath) == false) 
174
175{ 
176
177//  服务器上没有这个文件 
178
179return; 
180
181} 
182
183Page.Response.Clear(); 
184
185Page.Response.AddHeader( "Content-Type", "image/gif" );  //  根据  MIME  的不同设置 
186
187Page.Response.AddHeader("Content-Disposition", "inline;filename=" + filePath); 
188
189Page.Response.WriteFile(filePath); 
190
191Page.Response.End();   
192  
193---</form>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus