文件上传及下载
|
源代码网整理以下文件上传 源代码网整理以下一. 在Form中一定要将encType设为"multipart/form-data": 软件开发网 www.mscto.com 源代码网整理以下二. 判断是否有文件上传了: 源代码网整理以下三. 判断上传文件MIMIE类型: 源代码网整理以下四. 保存上传的文件: 源代码网整理以下1. 文件可以通过File1.PostedFile.SaveAs(path) //path是服务器上的物理路径,来保存文件。 源代码网整理以下if(File1.PostedFile.ContentLength != 0) 源代码网整理以下{ 软件开发网 www.mscto.com 源代码网整理以下 StringBuilder myStr = new StringBuilder(); 源代码网整理以下 myStr.Append("文件名称:" + File1.PostedFile.FileName); 软件开发网 www.mscto.com 源代码网整理以下 myStr.Append("<br>"); 源代码网整理以下 myStr.Append("文件类型:" + File1.PostedFile.ContentType); 源代码网整理以下 myStr.Append("<br>"); 软件开发网 www.mscto.com 源代码网整理以下 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; 软件开发网 www.mscto.com
源代码网整理以下 return; 软件开发网 www.mscto.com 源代码网整理以下 } 源代码网整理以下 File1.PostedFile.SaveAs(path); 源代码网整理以下 源代码网整理以下 myStr.Append("保存完毕!"); 源代码网整理以下 myStr.Append("<br>"); 源代码网整理以下 Label1.Text = myStr.ToString(); 源代码网整理以下} 软件开发网 www.mscto.com 源代码网整理以下else 源代码网整理以下{ 源代码网整理以下 Label1.Text = "你没有选择要上载的文件或者上传的文件长度为0!"; 源代码网整理以下} 源代码网整理以下 源代码网整理以下 源代码网整理以下文件下载 源代码网整理以下一. 服务端通过Response输出相应的HTTP Response Headers信息,和要下载的文件的数据来把文件发送到客户端,HTTP Response Headers表现在html文件中是下面的形式: 源代码网整理以下二. 首先,要输出文件的MIME类型: 源代码网整理以下三. 其次,要输出下载的文件的打开位置和文件名: 源代码网整理以下四. 准备发送到客户端的文件数据: 源代码网整理以下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数组 源代码网整理以下 源代码网整理以下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(); 软件开发网 www.mscto.com
源代码网整理以下SqlDataReader dr = command.ExecuteReader(); 源代码网整理以下if ( dr.Read()) 软件开发网 www.mscto.com
源代码网整理以下{ 源代码网整理以下 byte[] fileCont = (byte[]) dr["img_data"] ; 软件开发网 www.mscto.com
源代码网整理以下} 源代码网整理以下connection.Close(); 源代码网整理以下 源代码网整理以下HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse(); 源代码网整理以下Stream readStream = myWebResponse.GetResponseStream(); 源代码网整理以下byte[] bytes = new byte[readStream.Length]; 软件开发网 www.mscto.com
源代码网整理以下bytes = readStream.Read(bytes,0,readStream.Length); 源代码网整理以下通过上述三种方法获得的文件内容的byte数组就可以用来输出了: 源代码网整理以下Page.Response.End(); 源代码网整理以下 源代码网整理以下2. 直接读取文件输出: string FileName; //生成或获取要发送到客户端的文件名 源代码网整理以下string filePath = Server.MapPath("./") + FileName; //假设文件在当前目录下 源代码网整理以下if(File.Exists(filePath) == false) 软件开发网 www.mscto.com 源代码网整理以下{ 源代码网整理以下 //服务器上没有这个文件 源代码网整理以下 return; 源代码网整理以下} 软件开发网 www.mscto.com 源代码网整理以下Page.Response.Clear(); 软件开发网 www.mscto.com
源代码网整理以下Page.Response.AddHeader( "Content-Type", "image/gif" ); //根据MIME的不同设置 源代码网整理以下Page.Response.AddHeader("Content-Disposition", "inline;filename=" + filePath); 源代码网整理以下Page.Response.WriteFile(filePath); 源代码网整理以下Page.Response.End(); 源代码网推荐 源代码网供稿. |
